Luke
Luke

Reputation: 51

Read HTML code from iframe using Webbrowser C#

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;

Upvotes: 5

Views: 29997

Answers (4)

Rusty Nail
Rusty Nail

Reputation: 2710

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.

Upvotes: 0

o17t H1H' S'k
o17t H1H' S'k

Reputation: 2744

try:

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

Upvotes: 1

TheHolyTerrah
TheHolyTerrah

Reputation: 2879

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.

Upvotes: 1

user474407
user474407

Reputation:

Try:

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

Upvotes: 4

Related Questions