在没有ID或名称的输入提交上触发“点击”事件?

时间:2017-07-08 12:56:25

标签: c# html .net

我正在尝试使用.NET的Web浏览器在C#语言中自动填充网页上的表单。我需要触发提交按钮,但它没有名称或ID,它只有一个类型和值。

类型等于“提交”,值等于“登录”

以下是我为注册做的事情:

webBrowser1.Document.GetElementById("email").InnerText = email;
webBrowser1.Document.GetElementById("password").InnerText = password;
webBrowser1.Document.GetElementsByTagName("input")["register"].InvokeMember("click");

但这一次,提交按钮没有名称,有人可以指导我按正确的方向获取元素类型甚至更好,价值吗?

HTML:

<form accept-charset="utf-8" method="post">
    <div class="field">
        <label for="email">Email or Username</label> <span class="required">*</span> <input class="email" id="email" name="email" size="32" type="text">
    </div>
    <div class="field">
        <label for="password">Password</label> <span class="required">*</span> <input id="password" name="password" size="16" type="password">
        <p class="tip"><a href="/signin/reset" id="reset_password_link">I don't know my password</a></p>
    </div>
    <div class="field">
        <input id="persistent_hidden" name="persistent" type="hidden" value="f"> <input checked="checked" id="persistent" name="persistent" type="checkbox" value="t"> <label for="persistent">Keep me logged in</label>
    </div>
    <div class="submit">
        <input id="redirect_path" name="redirect_path" type="hidden"> <input type="submit" value="Sign In"> or <a class="cancel" href="/">Cancel</a>
    </div>
</form>

1 个答案:

答案 0 :(得分:1)

尝试:

foreach(HtmlElement elem in webBrowser.Document.getElementByTagName("input")) {
    if (elem.GetAttribute("value") == "Sign In") {
        elem.InvokeMember("Click");
    }
 }