C#中的mshtml.HTMLDocumentClass

时间:2009-07-15 19:46:09

标签: c# html mshtml dom

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

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

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

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

但我网址的结构比那复杂100倍。

假设网址是yahoo.com,我想“点击”网页搜索按钮。

有任何系统的解决方法吗?

1 个答案:

答案 0 :(得分: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 负责处理。