Read HTML code from iframe using Webbrowser C#

时光总嘲笑我的痴心妄想 提交于 2020-01-02 04:54:13

问题


How to read IFRAME html code using WebBrowser?

I have site with iframe, and after few clicks new URL opens inside this IFRAME with some portion of HTML CODE. Is there a possiblity to read this?. When I am trying to Navigate() to this URL, I am redirected to main page of this site (it is not possible to open this link twice).

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

Maybe there is something similar to:

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

回答1:


Try:

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



回答2:


You can also acquire various items via mshtml types:

Set a reference to the "Microsoft HTML Object Library" under COM references.

Set your using statement:

using mshtml;

Then tap into the mshtml API to snatch the source:

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

If "frame" isn't null after that line, it has a lot of items hanging off it for your use.




回答3:


try:

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




回答4:


A WebBrowser Control window can contain more that one iframe and .net supports frame collection so why not use something like this:

// 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;
}

This way, maybe with a little adjustment you can get all you need from the iframe containers you need.



来源:https://stackoverflow.com/questions/4146184/read-html-code-from-iframe-using-webbrowser-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!