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

C# 使用Webbrowser C从iframe读取HTML代码#,c#,iframe,browser,C#,Iframe,Browser,如何使用WebBrowser读取IFRAME html代码 我有一个带有iframe的站点,点击几下后,新的URL就会在这个iframe中打开,其中包含一些HTML代码。有可能读到这个吗?。当我尝试导航()到此URL时,我被重定向到此网站的主页(不可能打开此链接两次) 也许有类似的东西: Uri IFRAME_URL = webBrowser1.Document.Window.Frames[0]. ... DOCUMENTTEXT; 尝试: 您还可以通过mshtml类型获取各种项目: 在CO

如何使用WebBrowser读取IFRAME html代码

我有一个带有iframe的站点,点击几下后,新的URL就会在这个iframe中打开,其中包含一些HTML代码。有可能读到这个吗?。当我尝试导航()到此URL时,我被重定向到此网站的主页(不可能打开此链接两次)

也许有类似的东西:

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


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

在COM引用下设置对“Microsoft HTML对象库”的引用

设置您的using语句:

using mshtml;
然后点击mshtml API获取源代码:

HTMLFrameBase frame = yourWebBrowserControl.Document.GetElementById( "yourFrameId" ).DomElement as HTMLFrameBase;
如果该行后面的“frame”不是空的,那么它会挂起许多项目供您使用。

试试:


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容器中获得所需的所有内容。

string content=webBrowser1.Document.Window.Frames[0].WindowFrameElement.InnerText;我使用了它并显示了内容。我收到了iframe的URL;-)啊,对不起,我的意思是“内容”作为变量。我收到的不是HTML代码(源代码),而是iframe的URL。不过还是要谢谢你!这是因为限制帧间访问以避免跨站点脚本攻击的限制。@Kiquenet看到这个答案了吗
HTMLFrameBase frame = yourWebBrowserControl.Document.GetElementById( "yourFrameId" ).DomElement as HTMLFrameBase;
// 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;
}