Webbrowser SetAttribute 不起作用(密码字段)
尝试编写一个程序,让我在 C# 的网络浏览器中自动登录。这是我目前用于此目的的代码:
HtmlElementCollection pageTextElements = loginBrowser.Document.GetElementsByTagName("input");
foreach (HtmlElement element in pageTextElements)
{
if (element.Name.Equals("username"))
element.SetAttribute("value", this.UserName);
if (element.Name.Equals("password"))
element.SetAttribute("value", this.Password);
}
它填写用户名,但不填写密码? ): 谷歌搜索了一下,但只有少数人提出了这个话题,但没有人回复。 /:
希望有人能帮助我。 这是密码字段的来源:
<input type="password" value="" maxlength="50" size="25" name="password" class="bginput">
tried to write a program which logs me in automatically in a webbrowser in c#. This is the code i use at the moment for this purpose:
HtmlElementCollection pageTextElements = loginBrowser.Document.GetElementsByTagName("input");
foreach (HtmlElement element in pageTextElements)
{
if (element.Name.Equals("username"))
element.SetAttribute("value", this.UserName);
if (element.Name.Equals("password"))
element.SetAttribute("value", this.Password);
}
It fills in the Username, but not the password? ):
Googled around but there are only a few people which started topic to which no one ever replied. /:
hopefully someone can help me.
this is the source auf the password field:
<input type="password" value="" maxlength="50" size="25" name="password" class="bginput">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
上述方法都不适合我,我可以在 DocumentCompleted() 事件处理程序中的用户名文本框中调用 setAttribute(),但不能在密码文本框中调用 setAttribute()。我最终让它工作:
然后在 onFocus 处理程序中:
我不知道为什么这个有效而另一个不起作用。我只尝试更改密码,以防文档因设置用户名而发生更改而干扰它。我什至创建了另一个 WebBrowser 控件,然后从源中获取 DocumentText,在原始 html 中查找并替换设置密码值,然后在第二个 WebBrowser 上设置 DocumentText,但它再次没有正确设置该值。
如果有人有的话,我很想知道一种更清洁的解决方案
None of the above worked for me, I could call setAttribute() on the username textbox in the DocumentCompleted() event handler but not the password text box. I eventually got it to work by:
Then in onFocus handler:
I have no idea why this works and the other doesn't. I tried only changing the password just in case the document change from setting the username interfered with it. I even went to far as to create another WebBrowser control, then took the DocumentText from the source, did a find and replace setting the password value in the raw html before setting the DocumentText on the second WebBrowser and it again did not set the value properly.
I would love to know a cleaner solution if anyone has one
您需要等待文档更新完成。
DocumentCompleted
事件方法。如果您想查看正在发生的情况,请创建一个顶部有
Panel
、底部有WebBrowser
的表单。添加 3 个TextBoxes
、一个Button
和另一个TextBox
。以下框的OnClick
方法执行以下操作:您将看到表单上的
Password
框已正确填充。韦恩
You need to wait until the document updating has been completed.
DocumentCompleted
event method.If you want to see what is going on create a form with a
Panel
on top and aWebBrowser
on the bottom. Add 3TextBoxes
, aButton
and anotherTextBox
. TheOnClick
method of the following box do the following:You'll see that your
Password
box on you form populates correctly.Wayne
尝试像这样设置innerText属性,它对我有用(vb.net):
try setting the innerText property like this, it works for me (vb.net):