Auto fill textbox in a website when multiple Id tags are the same c#

2

I would like to fill textbox in a website via my c# program

My problem is that the Id tag for the textbox i want to fill is the same as the Id tag as another textbox and the one I want to fill is not the first

I can get to the HemlElment i need with the code

var oForm = This.Document.Forms[2];

This is the innerhtml of the oForm and I need to fill in the username, password, email and timezone.

<INPUT style="WIDTH: 150px" id=username name=username_reg size=30>
<INPUT style="WIDTH: 150px" id=password name=password_reg value="" size=30 type=password>
<INPUT style="WIDTH: 150px" id=mail_reg name=mail_reg size=30>

SELECT name=time_zone

If I try

oForm.SetAttribute("username_reg", RegisterData.UserData.Username);

there is an exception, Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))

Any one have any idea what to do?

c#
webbrowser-control
getelementbyid
getelementsbytagname
getelementsbyclassname
asked on Stack Overflow Oct 18, 2012 by Roey Moyal • edited Oct 18, 2012 by J. Steen

1 Answer

0

Try:

    var form = webBrowser1.Document.Forms[2];

    FindFormInputElementByIdAndSetItsValue(form, "username", "{{UserName}}");
    FindFormInputElementByIdAndSetItsValue(form, "password", "{{Password}}");
    FindFormInputElementByIdAndSetItsValue(form, "mail_reg", "{{EMail}}");

...

    public void FindFormInputElementByIdAndSetItsValue(
                 HtmlElement form, string id, string value)
    {
        form.GetElementsByTagName("input")
            .OfType<System.Windows.Forms.HtmlElement>()
            .Where(x => x.Id == id) 
            .First()
            .SetAttribute("value", value);
    }
answered on Stack Overflow Jul 20, 2014 by ShamilS

User contributions licensed under CC BY-SA 3.0