使用Webbrowser C#从iframe读取HTML代码

如何使用WebBrowser阅读IFRAME html代码?

我有iframe的网站,点击几下后,新的URL在这个IFRAME中打开了HTML CODE的一部分。 有没有可能读到这个? 当我尝试Navigate()到此URL时,我被重定向到此站点的主页(无法打开此链接两次)。

Uri IFRAME_URL = webBrowser1.Document.Window.Frames[0].Url; 

也许有类似的东西:

 Uri IFRAME_URL = webBrowser1.Document.Window.Frames[0]. ... DOCUMENTTEXT; 

尝试:

 string content = webBrowser1.Document.Window.Frames[0].WindowFrameElement.InnerText; 

您还可以通过mshtml类型获取各种项目:

在COM引用下设置对“Microsoft HTML Object Library”的引用。

设置你的使用声明:

 using mshtml; 

然后点击mshtml API来抓取源代码:

 HTMLFrameBase frame = yourWebBrowserControl.Document.GetElementById( "yourFrameId" ).DomElement as HTMLFrameBase; 

如果“line”在该行之后不为空,则它会挂起很多项目供您使用。

尝试:

string content = webBrowser1.Document.Window.Frames [0] .Document.Body.InnerText

WebBrowser控件窗口可以包含多个iframe和.net支持帧集合,所以为什么不使用这样的东西:

 // Setup a string variable... string html = string.Empty; // webBrowser1.Document.Window.Frames gets a collection of iframes contained in the current document... // HTMLWindow is the iterator for the Collection... foreach (HtmlWindow frame in webBrowser1.Document.Window.Frames) { html += frame.Document.Body.OuterHtml; } 

这样,通过一些调整,您可以从所需的iframe容器中获得所需的一切。