5

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;
Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Luke
  • 51
  • 1
  • 1
  • 2
  • 1
    @Kiquenet see this answer http://stackoverflow.com/questions/20013498/webbrowser-iframe-access-causing-unauthorized-access/20494120#20494120 – David Thompson Dec 10 '13 at 18:25

4 Answers4

4

Try:

string content = webBrowser1.Document.Window.Frames[0].WindowFrameElement.InnerText;
  • string content = webBrowser1.Document.Window.Frames[0].WindowFrameElement.InnerText; – Luke Nov 10 '10 at 16:49
  • I used this and displayed content. I received URL of iframe ;-) – Luke Nov 10 '10 at 16:50
  • Ah, sorry I meant "content" as variable. Instead of HTML code (source) I received URL of my iframe. But thanks anyway! – Luke Nov 10 '10 at 22:28
1

try:

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

o17t H1H' S'k
  • 2,541
  • 5
  • 31
  • 52
1

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.

TheHolyTerrah
  • 2,859
  • 3
  • 43
  • 50
0

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.

Rusty Nail
  • 2,692
  • 3
  • 34
  • 55