[RESOLVED] [2010] Clicking webbrowser element, only working once per program startup..
I am experiencing a really, really annoying piece of code. Frankly, I can't understand why it won't work, or the several workarounds...
I am automating a bunch of sites for filling out data (it's NOT malicious in any way). The one section of pages I am working on currently was going smooth, until about a day ago. I can't seem to click the button to make it go to the next page after the first time through successfully.
Here's a simple example:
Code:
//This is in the main form
new FormNameHere(param1).ShowDialog();
Code:
//FormNameHere
private void FormNameHere_Load(object sender, EventArgs e)
{
if(browser == null)
browser = new Browser();
else
{
browser.Dispose();
browser = new Browser();
}
browser.DocumentCompleted += new BrowserDocumentComletedEventHander(Browser_DocumentCompleted);
//navigate to first page to fill out
browser.Navigate
}
private void Browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//determine what page we are one
switch(page)
{
case 1:
//fill out and click next
browser.FirstPage();
break;
......
case 10:
browser.TenthPage();
break;
}
}
Code:
//Browser, inherits WebBrowser
public void TenthPage()
{
//fill out everthing needed
//click to continue to next page
HtmlElementCollection htmlcol = this.Document.GetElementsByTageName("img");
for(int i=0; i<htmlcol.Count; i++)
{
if(htmlcol[i].GetAttribute("alt").Contains("Continue")) //click this element
{
htmlcol[i].InvokeMember("click");
break;
}
}
}
Now, you can see that each time I click the button on the main form, I create an entire new instance of the browser form. The browser form also creates an entirely new instance of the browser.
The very first time I run through this part of my program, the TenthPage clicks the button and navigating continues. However, if I run this code again without stopping and restarting debugging inside Visual Studio completely, the TenthPage clicks the button but it doesn't navigate anywhere like it did before.
It sounds confusing and it is. I included a button on the browser form with exact same click code
Code:
HtmlElementCollection htmlcol = browser.Document.GetElementsByTagName("img");
for (int i = 0; i < htmlcol.Count; i++)
{
if (htmlcol[i].GetAttribute("alt").Contains("Continue"))
{
htmlcol[i].InvokeMember("click");
break;
}
}
and when manually clicking it successfully navigates to the next page. When calling the button_click via code in a timer spawned during the TenthPage method, I get a "Specified cast invalid" error on
Code:
HtmlElementCollection htmlcol = this.Document.GetElementsByTageName("img");
.
I am beyond confused at why clicking a button manually works, but calling the same code behind the button via a timer brings up that error. It's the same code regardless where it executed from....
*I'm unable to provide the pages I am filling out, so I hope I provided enough information.
Re: [2010] Clicking webbrowser element, only working once per program startup..
Sorry for all the confusion on this if it caused any. I actually tracked down the issue of the code inside the button click erroring from being executed inside a timer. For the longest time, I was confused, wondering why it worked when clicked, but not from a timer. The timer was causing a green debug line with the message "Specified Cast Invalid" when trying to get the HtmlElementCollection.
Turns out, I was running a System.Timer.Timers instead of a System.Windows.Forms.Timer. I was unsure why this was an issue as I didn't experience any cross threading issues. Also, I've accessed the webbrowser documents from other threads before. Well, at least after changing it to a windows forms timer it works. Hope this could help someone else!