3

我已经创建了网络浏览器,现在我想通过单击表单上的 button1 单击网络上的按钮。

private void button1_Click(object sender, EventArgs e)
{
    webBrowser1.Document.GetElementById("search").SetAttribute("value", IP.Text);
    webBrowser1.Document.GetElementById("").InvokeMember("click");
}

但我无法从此字符串中获取按钮 ID

<input type="submit" value="→" class="button">

有没有办法解决这个问题?

4

2 回答 2

6

尝试迭代<input>网页中的所有元素。当您找到具有所需值的元素时,您可以单击该元素。

foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("input")
{
    if (el.GetAttribute("value").Equals("→"))
    {
        el.InvokeMember("click");
    }
}
于 2012-08-29T17:03:07.130 回答
4

The website doesn't prevent POSTing a form to it. So you could always just create your own webpage with the form that points to it.

HTML

<form id="search" action="http://whois-service.ru/lookup/" method="post">
    <input type="text" class="text" name="domain" id="domain" />
    <input type="submit" value="" class="button" />
    <input id="submit" type="hidden" name="real" value="true2.1simpleJ" />
</form>

c#

private void button1_Click(object sender, EventArgs e)
{
    webBrowser1.DocumentText = "[The HTML from above here]";
    webBrowser1.Document.GetElementById("domain").SetAttribute("value", IP.Text);
    webBrowser1.Document.GetElementById("submit").InvokeMember("click");
}

If you are planning on scraping the WHOIS information. You must ensure it's not against the terms and conditions to do so. Most WHOIS services don't allow automated checks.

If they do and that's what you are trying to do then you should consider using HttpWebRequest instead, it's much more efficient. See an example here on how to use http://swhobby.blogspot.co.uk/2010/03/c-example-of-http-post.html

于 2012-08-29T16:58:19.367 回答