如何通过CSharp以编程方式设置输入元素的值?

Nab*_*ING 5 html c# webforms autofill browser-automation

您好我试图自动化我的IE登录到一个网站,但问题是输入元素没有HTML ID属性!例如:

<input type="text" name="user" size="15" value="">

如何编写C#以在此文本框中插入文本?

谢谢

bev*_*qua 6

将以下属性添加到输入标记:runat="server"id="someId"

<input id="user" type="text" size="15" runat="server">
Run Code Online (Sandbox Code Playgroud)

然后服务器端你做:

user.Text = "sample text";
Run Code Online (Sandbox Code Playgroud)

然后你可以这样做:

foreach (Control c in Page.Controls)
{
    TextBox t = c as TextBox;

    if (t != null)
    {
        t.Text = "sample text";
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我不确定没有runat="server"属性它会工作