0

After some research on the WebBrowser DocumentCompleted issue, I've inserted my login attempt into the DocumentCompleted Event Handler.

Here's my code:

public void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        wb.Navigate("fooPage");
        var browser = (WebBrowser)sender;

        HtmlElement email = CookieReader.GetElement("email", browser);
        HtmlElement password = CookieReader.GetElement("pass", browser);
        email.SetAttribute("email", "foo@something.com");
        password.SetAttribute("pass", "foo");
        HtmlElement loginElement = CookieReader.GetElement("fooLog", browser);
        loginElement.InvokeMember("click"); //wb_DocumentCompleted Method Continues...

I didn't manage to login (double checked that I got the login button right). It seems that the problem is with the ReadyState property of the browser object. It's always loading, while the IsBusy property is always False. Also, the page was supposed to finish loading because the DocumentCompleted event fired. Any ideas how this even possible?

Moreover, when debugging, the InvokeMember method changes the html INPUT element and a disabled tag appears (disabled=\"\" - which is HTML5 disabled="disabled" if I'm not mistaken). I don't know why this tag is added (wasn't there before), and if it's related in any way to the permanent Loading ReadyState of the page... insights and/or advises will be much appreciated!

Maayan
  • 208
  • 2
  • 7

1 Answers1

0

I had the same problem. The DocumentCompleted event fires more that once in a single load. I think the first is not the real one. I think you should create a BackgroundWorker and start it when the DocumentCompleted event fires. It should wait a few seconds before do the job. That will definitely work.

You should start the BackgroundWorker only once.

Patartics Milán
  • 4,858
  • 4
  • 26
  • 32
  • I apologize in advance in case my question is extremely stupid (I'm new to C# and .NET): Wouldn't it'll be easier to use a Timer instead of a BackgroundWorker? Also, I've read the MSDN article concerning BackgroundWorker, but as I've never worked with threads (or with WebBrowser before) I'd be happy to get a more specific example if you can. Much appreciated! – Maayan May 16 '13 at 11:34
  • You can use a timer instead. Yes it's easier for simple programs. Just create a timer control (you can do it with the GUI too), set the interval property to 2000-3000. When the DocumentCompleted event fires first, start the timer. You have to add a function to the timer's tick event. When it ticks (after the interval) you should stop the timer to prevent further function calls. I think [this](http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/43daf8b2-67ad-4938-98f7-cae3eaa5e63f/) thread helps you to understand the usage of timers – Patartics Milán May 16 '13 at 11:49
  • I've tried it (learned lots of new things), and the timer's doing it's job perfectly - it delays the login attempt. But the page is still loading. That is - both ReadyState and IsBusy properties remained the same as before. – Maayan May 16 '13 at 14:04
  • Good to learn new stuff :) Maybe you can use the [geckofx](http://code.google.com/p/geckofx/) component for embedding Mozilla Gecko (Firefox) in .NET applications. It is well documented, and you will find threads about it here in SOF. It works better then the built-in IE based browser. – Patartics Milán May 16 '13 at 14:17
  • I solved the ReadyState issue: surprisingly, another url which redirects to the login page loads completely. Thank you so much for your time and help! I'm still getting a disabled html tag following execution of loginElemnt.InvokeMember("click"). Also, loginElement enabled property is rendered false. – Maayan May 16 '13 at 14:45
  • Your welcome! I know that web automation under c# .net is not as obvious as it seems. Please click the tick next to my answer if you think it was helpful. That gives me reputation. Cheers! – Patartics Milán May 16 '13 at 17:25