简体   繁体   English

当多个ID标签为同一C#时,自动填充网站中的文本框

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

I would like to fill textbox in a website via my c# program 我想通过我的C#程序在网站中填写文本框

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 我的问题是我要填充的文本框的Id标签与另一个文本框的Id标签相同,而我要填充的文本框不是第一个

I can get to the HemlElment i need with the code 我可以通过代码找到需要的HemlElment

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. 这是oForm的innerhtml,我需要填写用户名,密码,电子邮件和时区。

<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)) 有一个例外, Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND)) Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))

Any one have any idea what to do? 有人知道该怎么办吗?

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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM