2

在 C# 中,我设法从 InternetExplorer 对象(导航到某个 URL)中获取整个 HTMLDocumentClass。

但是,在 Visual Studio 2008 的调试模式下,这个特定 URL 的 HTMLDocumentClass 的内容是 MASSIVE,包括像 activeElement、alinkColor、all、applet、charset、childNodes 等等属性。

该页面中有一个按钮,我希望将其更改为“已单击”。但我不知道如何找到该按钮的名称/ID/标签。有一个简单的教程使用如下语句:

HTMLInputElement button =
  (HTMLInputElement)theDoc.getElementById("Button1");
button.click();

但是我的 URL 的结构比这复杂 100 倍。

假设 URL 是 yahoo.com,我想“单击”Web Search 按钮。

有什么系统的方法来解决这个问题吗?

4

1 回答 1

5

这是假设我的 WebBrowser 控件位于 Yahoo。搜索按钮的 id 是“searchsubmit”

使用 Windows.Forms.HtmlDocument

 HtmlElement button = (HtmlElement)htmlDoc.GetElementById("searchsubmit");
 button.InvokeMember("click");

如果使用 mshtml 和 HTMLInputElement

   HTMLDocument htmlDoc = new HTMLDocumentClass();
    htmlDoc = (HTMLDocument)axWebBrowser1.Document;

   //find the search text box..
   HTMLInputElement searchTextBox = (HTMLInputElement)htmlDoc.all.item("p", 0);
   searchTextBox.value = "Stack Overflow";

   //find the button
   HTMLInputElement searchButton = (HTMLInputElement)htmlDoc.all.item("searchsubmit", 0);
   searchButton.click();

如果您查看 Yahoo 源代码,您会看到搜索文本框位于多个 div 中。htmlDoc.all.item负责它。

于 2009-07-15T20:56:36.863 回答