views:

1161

answers:

1

I'm trying to use a .Net WebBrowser control to log into a website and aggregate some data for me. I'm also using a reference to MSHTML (but I don't care if your solution does or doesn't).

When I visit a 'normal' website - I create an mshtml.HTMLDocument from the webBrowser's .documment. Then I create a mshtml.FormElement and a mshtml.HTMLInputElement using the name of the textbox I want.

Then, I can set the value of the textBox.

It looks something like this:

    Dim myDoc1 As mshtml.HTMLIFrame = DirectCast(Me.WebBrowser.Document.DomDocument, mshtml.HTMLDocument)
    Dim myForm As mshtml.HTMLFormElement = DirectCast(myDoc1.forms.item(0), mshtml.HTMLFormElement)
    Dim myUserBox As mshtml.HTMLInputElement = DirectCast(myForm.item("user"), mshtml.HTMLFormElement)

    myUserBox.value = UserName

My problem is that on some websites the textbox I need to manipulate is contained inside of an IFRAME. An example would be https://servicing.capitalone.com/c1/login.aspx

If my program surfs to that site - it can't find a textbox for the Username - because there isn't one. There is only an IFRAME tag that has a URL of 'https://login.capitalone.com/loginweb/login/login.do'

If I surf directly to the login.do page - the website redirects me to an error page.

How can I enter data into the UserName textbox in that IFRAME?

+2  A: 

Because the child iframe you are trying to access is coming from a different domain than the parent page, you won't be able to access the form elements of the iframe due to cross-frame scripting security. See: Accessing Frames in the Managed HTML Document Object Model

Access to frames is complicated by the fact that the managed HTML DOM implements a security measure known as cross-frame scripting security. If a document contains a FRAMESET with two or more FRAMEs in different domains, these FRAMEs cannot interact with one another. In other words, a FRAME that displays content from your Web site cannot access information in a FRAME that hosts a third-party site such as http://www.adatum.com/. This security is implemented at the level of the HtmlWindow class. You can obtain general information about a FRAME hosting another Web site, such as its URL, but you will be unable to access its Document or change the size or location of its hosting FRAME or IFRAME.

Jason DeFontes
So - there is no way to programatically log into the Capital One Website using a WebBrowser control?
Rob P.
I couldn't get it to work. There is an API for accessing the iframes, but any time I touched the child frame I got a security exception.
Jason DeFontes
I've hacked together a solution. I'm using SendKeys to tab a few times until I'm in the right field, then enter a username, tab, password, tab, click the submit button. But this is buggy and prone to errors. If anyone else has a better soltuion I'd be interested to hear it :)
Rob P.
Yeah, I guess going through the front door would work. ;-)
Jason DeFontes