Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webbrowser SetAttribute not working (Password Field)

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">
like image 335
Omegavirus Avatar asked Jan 21 '23 00:01

Omegavirus


1 Answers

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

like image 67
Dan Avatar answered Jan 31 '23 05:01

Dan