Webbrowser SetAttribute not working (Password Field)

醉酒当歌 提交于 2019-12-23 19:08:06

问题


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

回答1:


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




回答2:


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




回答3:


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"


来源:https://stackoverflow.com/questions/4269769/webbrowser-setattribute-not-working-password-field

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