C#-使用WebBrowser自动登录到网页-禁用html标签 [英] C# - Automatic Login to a webpage using WebBrowser - disabled html tag

查看:250
本文介绍了C#-使用WebBrowser自动登录到网页-禁用html标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对WebBrowser DocumentCompleted问题进行了研究之后,我将登录尝试插入到DocumentCompleted事件处理程序中。

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

这是我的代码:

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...

我没有登录(仔细检查我是否右侧的登录按钮)。看来问题出在浏览器对象的ReadyState属性。它始终处于加载状态,而IsBusy属性始终为False。另外,由于触发了DocumentCompleted事件,因此该页面应该完成了加载。任何想法怎么可能呢?

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?

此外,在调试时,InvokeMember方法会更改html INPUT元素并显示一个禁用标签(disabled = \ \-如果我没记错的话,就是HTML5 disable = disabled)。我不知道为什么添加此标签(以前不存在),并且如果它与页面的永久Loading ReadyState有任何关系,我们将不胜感激和/或建议!

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!

推荐答案

我遇到了同样的问题。 DocumentCompleted 事件在一次加载中触发的次数更多。我认为第一个不是真实的。我认为您应该创建一个 BackgroundWorker 并在 DocumentCompleted 事件触发时启动它。它应该等待几秒钟,然后再执行此工作。

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.

您应该只启动一次 BackgroundWorker

这篇关于C#-使用WebBrowser自动登录到网页-禁用html标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆