Witam, próbuję uruchomić klikanie w divy przy użyciu webbrowser w osobnym thredzie lecz otrzymuje błąd podczas gdy pętla foreach zaczyna bawić się z przeglądarką kod:
Blad pojawia się w linijce: foreach (String s in u.Document.All)
A funkcję Like odpalam za pomocą:
LikeThread = new Thread(() => Like(webBrowser1));
LikeThread.Start();
public void Like(WebBrowser u)
{
do
{
iloscdorefa++;
//lajkowanie
foreach (String s in u.Document.All)
{
MessageBox.Show("" + s);
}
HtmlElementCollection lol = u.Document.GetElementsByTagName("like hintable");
foreach (HtmlElement ee in lol)
if (ee.GetAttribute("className") == "like hintable")
{
ee.InvokeMember("click");
break;
}
//foreach (HtmlElement ee in webBrowser1.Document.All)
// if (ee.GetAttribute("className") == "like hintable")
// {
// ee.InvokeMember("click");
// break;
// }
//rozwijanie
if (iloscdorefa == 10)
{
foreach (HtmlElement ee in u.Document.All)
if (ee.GetAttribute("className") == "submit-button-more submit-button-more-active")
ee.InvokeMember("click");
//Thread.Sleep(rnd.Next(500, 2000));
//foreach (HtmlElement ee in webBrowser1.Document.All)
// if (ee.GetAttribute("className") == "submit-button-more submit-button-more-active")
// ee.InvokeMember("click");
iloscdorefa = 0;
}
Thread.Sleep(rnd.Next(1000, 3500));
}while (Dziala);
}
Blad:
System.InvalidCastException was unhandled
Message=Określone rzutowanie jest nieprawidłowe.
Source=System.Windows.Forms
StackTrace:
w System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.GetLocation()
w System.Windows.Forms.WebBrowser.get_Document()
w AutomatyczneLajki.Form1.Like(WebBrowser u) w C:\Szkola\AutomatyczneLajki\AutomatyczneLajki\Form1.cs:wiersz 38
w AutomatyczneLajki.Form1.<butLikeAll_Click>b__0() w C:\Szkola\AutomatyczneLajki\AutomatyczneLajki\Form1.cs:wiersz 124
w System.Threading.ThreadHelper.ThreadStart_Context(Object state)
w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
w System.Threading.ThreadHelper.ThreadStart()
InnerException: