Frames in MSHTML

D

David Pendrey

Hello all,

I am using the WebBrowser control to browse a webpage containing frames and am having difficulties accessing the 'frames' property of a document object. Bellow is my code and the error recieved:
webAccess.Navigate(URL);

// Wait until document is loaded. This part DOES work

IHTMLDocument2 mDoc = (IHTMLDocument2)(webAccess.Document);

IHTMLFramesCollection2 mFrames = (IHTMLFramesCollection2)(mDoc.frames);



The final line of code returns the error of:

An unhandled exception of type 'System.InvalidCastException' occurred in xxxx.exe

Additional information: No such interface supported



Does anyone out there know of a way to fix this? It would be greatly apreciated.
 
C

Chris Priede

Hi,

David said:
I am using the WebBrowser control to browse a webpage containing
frames and am having difficulties accessing the 'frames' property of
a document object.
An unhandled exception of type 'System.InvalidCastException'
occurred in xxxx.exe
Does anyone out there know of a way to fix this? It would be greatly
apreciated.

Here's an excerpt of actual working code I wrote a while ago, which drills
down to the document contained in a frame named "main", and does things with
its contents. Hopefully that will help you:

mshtml.HTMLDocument doc = (mshtml.HTMLDocument)browser.Document;
mshtml.FramesCollection frames = (mshtml.FramesCollection)doc.frames;
mshtml.IHTMLWindow2 mainFrame = null;
for (int i = 0; i < frames.length; i++)
{
object refIndex = i;
mshtml.IHTMLWindow2 frame = (mshtml.IHTMLWindow2)frames.item(ref
refIndex);
if (frame.name == "main")
mainFrame = frame;
}
if (mainFrame != null)
{
mshtml.IHTMLDocument2 mainFrameDoc = mainFrame.document;
[...]
}

As I recall, I too encountered numerous invalid cast errors while trying to
interpret and folow the sparse documentation on this topic. I ended up
writing code to dump the actual type of all objects in the tree and referred
to that.
 
D

David Pendrey

Hi,

Thanks, that code is working better but still not well enough unfortunatly. I've fiddled around a bit and your code works fine when it's in the code being pressed by a button after you press a button to load the page. However if you have code to go to a page, then sleep until ReadyState == SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE and then use your code it doesn't work. I tried to wait a while (up to 5 seconds) and running your code in a seperate thread with no luck. Any ideas on this?

David Pendrey



Chris Priede said:
Hi,

David said:
I am using the WebBrowser control to browse a webpage containing
frames and am having difficulties accessing the 'frames' property of
a document object.
An unhandled exception of type 'System.InvalidCastException'
occurred in xxxx.exe
Does anyone out there know of a way to fix this? It would be greatly
apreciated.

Here's an excerpt of actual working code I wrote a while ago, which drills
down to the document contained in a frame named "main", and does things with
its contents. Hopefully that will help you:

mshtml.HTMLDocument doc = (mshtml.HTMLDocument)browser.Document;
mshtml.FramesCollection frames = (mshtml.FramesCollection)doc.frames;
mshtml.IHTMLWindow2 mainFrame = null;
for (int i = 0; i < frames.length; i++)
{
object refIndex = i;
mshtml.IHTMLWindow2 frame = (mshtml.IHTMLWindow2)frames.item(ref
refIndex);
if (frame.name == "main")
mainFrame = frame;
}
if (mainFrame != null)
{
mshtml.IHTMLDocument2 mainFrameDoc = mainFrame.document;
[...]
}

As I recall, I too encountered numerous invalid cast errors while trying to
interpret and folow the sparse documentation on this topic. I ended up
writing code to dump the actual type of all objects in the tree and referred
to that.
 
C

Chris Priede

David said:
Thanks, that code is working better but still not well enough
unfortunatly. I've fiddled around a bit and your code works fine when
it's in the code being pressed by a button after you press a button
to load the page. However if you have code to go to a page, then
sleep until ReadyState == SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE
and then use your code it doesn't work. I tried to wait a while (up
to 5 seconds) and running your code in a seperate thread with no
luck. Any ideas on this?

My project was an automated sequence of about half a dozen steps, which logs
in to a site with a username/password, navigates to a specific page, submits
a search form on that page, and scrubs data from the results. The sequence
is driven by a timer and a state machine. Completion, timeout and errors
are detected through DocumentComplete and NavigateError events. This might
not be the greatest design, but it seems to work well enough under both
normal and error circumstances.

I never attempted to use ReadyState to detect completion. This is not to
say it wouldn't work, though.

If you use a separate thread, make sure you are using Invoke/BeginInvoke for
anything having to do with the browser control. It also appears necessary
to do so within any handlers for the browser control's events.
 
D

David Pendrey

Thank you for the assistance. I was running my code from invisde a seperate
thread from the main application so that the window would be automatically
redrawn and this appears to have been causing the problems. Now I run it all
in the same thread and simply use the following function to load a page and
wait until it is fully loaded:

protected HTMLDocument GotoPage(string URL)

{

while (webAccess.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)

{

System.Windows.Forms.Application.DoEvents();

System.Threading.Thread.Sleep(250);

}

webAccess.Navigate(URL);

while (webAccess.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)

{

System.Windows.Forms.Application.DoEvents();

System.Threading.Thread.Sleep(250);

}

return (HTMLDocument)(webAccess.Document);

}


Thank you again for all of your help :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top