A-A+

获取WebBrowser控件中的网页内容

2014年09月11日 技术 暂无评论 阅读 3,221 次

HtmlDocument:提供对 WebBrowser 控件承载的 HTML 文档的顶级编程访问。属于命名空间System.Windows.Forms。在 System.Windows.Forms.dll中。

HtmlElement:表示网页内的一个 HTML 元素。属于命名空间System.Windows.Forms。在 System.Windows.Forms.dll中。

获取WebBrowser中的网页:

  1. HtmlDocument htmlDocument = this.webBrowser1.Document;

通过元素ID获取HTML元素:

  1. HtmlElement htmlElement = this.webBrowser1.Document.GetElementById(strElementID) ;

获取某一HTML元素的的样式名称:

  1. //GetAttribute() - 检索元素中已命名属性的值。
  2. htmlElement.GetAttribute("className")

获取指定HTML标记表示的元素集合:

  1. //获取网页中所有链接标签
  2. htmlDocument.GetElementsByTagName("a")
  3. //获取某个元素下的所有链接标签
  4. htmlDocument.GetElementsByTagName("a")

关于文档对象模型(DOM):可以通过HtmlDocument.DomDocument获取此HtmlDocument的非托管接口指针;通过HtmlElement.DomElement获取此元素的非托管接口指针。这样可以访问基础 COM 接口中的未公开属性或方法,为了使用非托管接口,需要将 MSHTML 库 (mshtml.dll) 导入到应用程序中。如何引用见下图:

mshtml

示例代码如下:

  1. if (webBrowser1.Document != null)
  2. {
  3.    mshtml.IHTMLDocument2 document = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;
  4.    mshtml.IHTMLElement element = document.body;
  5. }

给我留言

Copyright © 字痕随行 保留所有权利.   Theme  Ality

用户登录

分享到: