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; } }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.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; } } }
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 codeand 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 onCode: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; } }.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.




Reply With Quote