Wednesday, February 24, 2010

Programmer - accessing DOM trees in frames/iframes using mshtml in C#

Programmer Question

hi, I have to programmically load a web page that has frames in it and then grab the DOM tree inside those frames. I have tried quite some time, but i am still struck at the code. here it is,



        void grabDOMTest()
{
mshtml.HTMLDocument htmldoc = new mshtml.HTMLDocument();
IHTMLDocument2 htmldoc2 = (IHTMLDocument2)htmldoc;
htmldoc2.writeln("<html>");
htmldoc2.writeln("<frameset cols='25%,50%,25%'>");
htmldoc2.writeln("<frame src='about:blank'>");
htmldoc2.writeln("<frame src='about:blank'>");
htmldoc2.writeln("<frame src='about:blank'>");
htmldoc2.writeln("</frameset>");
htmldoc2.writeln("</html>");
htmldoc2.close();

mshtml.FramesCollection frames = htmldoc2.frames;
object o = null;
for (int i = 0; i <= frames.length - 1; i++)
{
o = i;
try
{
mshtml.IHTMLWindow2 fwin = (mshtml.IHTMLWindow2)frames.item(ref o); //COMException,HRESULT E_FAIL
}
catch (COMException e)
{
.....
}

//do sth
}
}


i keep getting exceptions in the line with frames.item.
how can i get it to work? or is there any alternative approach?

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails