MSHTM: Howto access the elements within an <IFRAME>

C

cweeks

I am using the Webbrowser control in VS 2005 C# Express to automate the
navigation of pages on a partner's web site. The first page contains
an <IFRAME> element:

<iframe style='width:40; height:40'; align='center' marginwidth='0'
marginheight='0'
scrolling='no' frameborder='0' id='xyzlogin' name='xyzlogin'
src='https://www.xyzcorp.com/accounts/ServiceLogin'>
</iframe>

Within that frame I need to access these three elements:

<input type="text" name="Email" value="" class="xyz le val" id="Email"
size="18">
<input type="password" name="Passwd" class="xyz le val" id="Passwd"
size="18">
<input type="submit" name="null" value="Sign in" class="xyz le button"

so I can programmatically: 1)enter the user ID and password, 2)click
the 'Sign in' button.

I thought maybe Webbrowser.Document.Window.Frames would be the way to
go but that did not seem to work (sorry, I'm not at my development
environment right now or I would provide more detail).

Frames is an HtmlWindowCollection. An HtmlWindow has a Document member
which is an HTMLDocument. So, would this work:

-Look through the Frames collection until a HtmlWindow named 'xyzlogin'
is found.
-Get the Document member for that window and extract the following
HTMLElements by ID: Email, Passwd.
-Set the InnerText properties for these elements.
-Find the button element (how? it does not have a valid name or id)
-Use InvokeMember("Click") to click the button.
 
C

cweeks

Well, nothing is ever as easy as it appears or as it should be, is it?

Here's a snippet of the code:

-----
HtmlElement frameElement = null;
HtmlWindow docWindow = webBrowser1.Document.Window;
bool bLoginAttempted = false;

foreach (HtmlWindow frameWindow in docWindow.Frames)
-----

I can see in the debugger that the Frames collection has 2 elements.
But the frameWindow object does not have any valid value in any of the
members. In the debugger the value for the Document member, for
example, says:

'frameWindow.Document' threw an exception of type
'System.UnauthorizedAccessException' System.Windows.Forms.HtmlDocument
{System.UnauthorizedAccessException}

What causes this? Why can't I just access each frame as a simple
HtmlElement object?
 
C

cweeks

Apparently I have some things to learn about processing HTML frames.

"Accessing Frames in the Managed HTML Document Object Model"
http://tinyurl.com/q7mx3

"About Cross-Frame Scripting and Security"
http://tinyurl.com/o0gp

My simple solution is to simply navigate directly to the URL referenced
in the <IFRAME> tag and process the returned document. That enables
me, in this case at least, to process the document as a top-level
document.
 

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