Webbrowser SetAttribute 不起作用(密码字段)

发布于 2024-10-04 09:53:13 字数 679 浏览 4 评论 0原文

尝试编写一个程序,让我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生生漫 2024-10-11 09:53:13

上述方法都不适合我,我可以在 DocumentCompleted() 事件处理程序中的用户名文本框中调用 setAttribute(),但不能在密码文本框中调用 setAttribute()。我最终让它工作:

HtmlElementCollection inputs = doc.GetElementsByTagName("input");
HtmlElement usr = inputs.GetElementsByName("username")[0];
usr.setAttribute("value", strUsername);

HtmlElement pwd = inputs.GetElementsByName("password")[0];
pwd.GotFocus += new HtmlElementEventHandler(pwd_GotFocus);
pwd.Focus();

然后在 onFocus 处理程序中:

void pwd_GotFocus(object sender, HtmlElementEventArgs e)
{
    HtmlElement pwd = (HtmlElement)sender;
    pwd.SetAttribute("value", strPassword);
}

我不知道为什么这个有效而另一个不起作用。我只尝试更改密码,以防文档因设置用户名而发生更改而干扰它。我什至创建了另一个 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:

HtmlElementCollection inputs = doc.GetElementsByTagName("input");
HtmlElement usr = inputs.GetElementsByName("username")[0];
usr.setAttribute("value", strUsername);

HtmlElement pwd = inputs.GetElementsByName("password")[0];
pwd.GotFocus += new HtmlElementEventHandler(pwd_GotFocus);
pwd.Focus();

Then in onFocus handler:

void pwd_GotFocus(object sender, HtmlElementEventArgs e)
{
    HtmlElement pwd = (HtmlElement)sender;
    pwd.SetAttribute("value", strPassword);
}

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

不必在意 2024-10-11 09:53:13

您需要等待文档更新完成。 DocumentCompleted 事件方法。

如果您想查看正在发生的情况,请创建一个顶部有 Panel 、底部有 WebBrowser 的表单。添加 3 个 TextBoxes、一个 Button 和另一个 TextBox。以下框的 OnClick 方法执行以下操作:

webBrowser1.Document.GetElementById(this.textBox1.Text).SetAttribute(this.textBox2.Text, this.textBox3.Text);
this.textBox4.Text = webBrowser1.Document.GetElementById(this.textBox1.Text).GetAttribute(this.textBox2.Text);

您将看到表单上的 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 a WebBrowser on the bottom. Add 3 TextBoxes, a Button and another TextBox. The OnClick method of the following box do the following:

webBrowser1.Document.GetElementById(this.textBox1.Text).SetAttribute(this.textBox2.Text, this.textBox3.Text);
this.textBox4.Text = webBrowser1.Document.GetElementById(this.textBox1.Text).GetAttribute(this.textBox2.Text);

You'll see that your Password box on you form populates correctly.

Wayne

停顿的约定 2024-10-11 09:53:13

尝试像这样设置innerText属性,它对我有用(vb.net):

Dim txtPassword As HtmlElement = browser.Document.GetElementById("ctl00_ContentPlaceHolder1_txtPassword")

txtPassword.InnerText = "123456"

try setting the innerText property like this, it works for me (vb.net):

Dim txtPassword As HtmlElement = browser.Document.GetElementById("ctl00_ContentPlaceHolder1_txtPassword")

txtPassword.InnerText = "123456"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文