C# - Automatic Login to a webpage using WebBrowser - disabled html tag

拟墨画扇 提交于 2019-12-11 19:17:50

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/16585355/c-sharp-automatic-login-to-a-webpage-using-webbrowser-disabled-html-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!