Search code examples
parsingframesmshtml

How can I get a frame's content with mshtml?


Here's the issue:

I have a hook in IE that reacts on WebBrowser.OnNavigateComplete2 event to parse the content of the document for some precise info.

That document contains frames, so I look into the HTMLDocument.frames. For each one, I look into the document.body.outerHTML property to check for the content.

Problem is, the string I'm looking for never shows there, whereas it is displayed in the finale page. So, am I looking in the wrong place? If it is displayed when the page is fully loaded, then it's downloaded at some point, right? But in which object should I look ?

BTW, I Don't know if that is of any importance, but the page I'm searching into comes from a ASP.NET application.

public void OnNavigateComplete2(object pDisp, ref object url)
{
    document = (HTMLDocument)webBrowser.Document;

    mshtml.FramesCollection frames = document.frames;
    for (int i = 0; i < frames.length; i++)
    {
        object refIdx = i;
        IHTMLWindow2 frame = (IHTMLWindow2)frames.item(ref refIdx);
        string frameContent = frame.document.body.outerHTML;
    }
}

Thank your for your help.


@rams This event is launched many times for each page, so I figured it was each time a framed is loaded, even if i don't get to catch the one I'm looking for. If not, what would be the event to catch the frames content?

What I want to do is detect some precise info on a precise frame, then save it. later, a web page is loaded triggered by some user action, where I need the info I got from parsing the frame.


Solution

  • Do you know the name/id of the frame you are looking for content? If so, in your navigateComplete2 event, can you get a reference to the frame like

    iFrame frm = document.frames(<your frame id>);
    
    int readyState=0;
    
    while(frm.readystate !=4){
    // do nothing. be careful to not create an endless loop
    }
    
    if(frm.readyState==4){
       // get your content now
    }
    

    HTH