C# 中的 mshtml.HTMLDocumentClass

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1133549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:01:23  来源:igfitidea点击:

mshtml.HTMLDocumentClass in C#

c#htmlmshtmldom

提问by Saobi

In C#, I managed to get the entire HTMLDocumentClass from an InternetExplorer object (navigating to a certain URL).

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

However, in Visual Studio 2008's debug mode, the content of this HTMLDocumentClass for this particular URL is MASSIVE, including attributes like activeElement, alinkColor, all, applets, charset, childNodes, etc, etc ,etc.

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

There's a button in that page that I want the to change to "Clicked". But I have no idea how to find the name/id/tag of that button. There's a simple tutorial that uses statements like :

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

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

But the structure of my URL is 100 times more complex than that.

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

Let's say the URL is yahoo.com, and I want to 'click' the Web Search button.

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

Any systematic way of going about this?

任何系统的方法来解决这个问题?

采纳答案by Stan R.

This is assuming my WebBrowser control is at Yahoo. The search button's id is "searchsubmit"

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

Using Windows.Forms.HtmlDocument

使用 Windows.Forms.HtmlDocument

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

If using mshtml and HTMLInputElement

如果使用 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();

If you look at Yahoo source, you will see that the search textbox is within multiple divs. htmlDoc.all.itemtakes care of it.

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