2

When I try to access this:

var anchors = webBrowser1.Document.Window.Frames[0].Document.GetElementsByTagName("a");

I get the unauthorized access exception. What is going on!? I can look through the whole document in object browser while exception is being thrown, I can also manually click through this iframe inside my webBrowser1, but when I try to access it inside of my app, I get error? What wizardy is this?

ojek
  • 9,680
  • 21
  • 71
  • 110
  • Do the main page and the page inside Iframe belong to the same domain? – Yuriy Galanter Nov 16 '13 at 01:44
  • @YuriyGalanter: I am not sure, why would that matter if webBrowser already downloaded the page? – ojek Nov 16 '13 at 01:58
  • 2
    I think u maybe experiencing Same Origin policy that not does allow cross domain page communication in normal browser. As far as webBrowser is concerned you are trying to access page from one domain from another – Yuriy Galanter Nov 16 '13 at 02:01
  • @YuriyGalanter: Are you maybe aware of any easy solutions to that? Imo this makes no sense at all, since all the html already has been loaded (including iframe's html), so it should allow me to look into it... – ojek Nov 16 '13 at 02:05
  • I think what may work is loading content of that iframe (via Iframe URL) into another WebBrowser control (it could be hidden) and manipulating Document of that second webBrowser directly – Yuriy Galanter Nov 16 '13 at 02:20
  • See if this http://stackoverflow.com/questions/7505515/any-workaround-to-get-text-in-an-iframe-on-another-domain-in-a-webbrowser?rq=1 helps (if you are looking for web scraping from inner frame) – Alexei Levenkov Nov 16 '13 at 06:03

1 Answers1

3

This is because the browser wont allow you to access iframes from another domain, this also occurs with https sites where the domain name is the same, fortunately there is a way around this.

You can get the content of an IFrame by using JS once the page has fully loaded.

First load the url of the page that has an iframe embedded:

webBrowser1.Navigate("https://www.example.com/pagecontaingiframe.html");
webBrowser1.DocumentCompleted += WebBrowserDocumentCompleted;

Then in the document completed event, check that the iframe url has loaded, not the original url we navigated to, once we have loaded that, use the eval function in javascript to run our own code to pull the content of the iframe.

void WebBrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    // Optionally check that the url is the original document
    // search for an iframe and its url here first.
    // Then store the name and url for the next step


    // And the magic begins
    if (e.Url.AbsolutePath != new Uri("https://www.example.com/iframeyouneedurl.html").AbsolutePath)
        return;

    webBrowser1.DocumentCompleted -= WebBrowserDocumentCompleted;

    string jCode = "var iframe = document.getElementsByName('iframe_element')[0]; var innerDoc = iframe.contentDocument || iframe.contentWindow.document; innerDoc.documentElement.innerHTML";
    string html = webBrowser1.Document.InvokeScript("eval", new object[] { jCode });
}
David Thompson
  • 2,902
  • 2
  • 18
  • 21
  • i put this all in form1_Load and it gives me an error – k961 Oct 07 '15 at 17:37
  • it still Gives me the same Error -> access denied i can get iframe src but i cant read contents http://paste.ubuntu.com/12708279/ – k961 Oct 07 '15 at 20:04