1

This is for a Desktop C# app in Visual Studio Express 2012.

I am using a webBrowser control to logon to various websites however I am unable to retrieve and set the attributes for this particular website where the logon attributes are in an .asp page which is 'called' by the HTML.

The attributes themselves do not appear visible via GetElementById or navigation through the HtmlElementCollection.

The HTML snippet below shows the call to login.asp and the fields "CorporateSignonCorpId" and "CorporateSignonPassword" which I am trying to set but which reside in "login.asp"

**Website HTML**
</head>
<frameset rows="*,0">
<frame src="login.asp" id="main" name="main" frameborder="0" noresize="true" scrolling="auto" hidefocus="true" target="_top">
<frame src="hiddenframe.asp" id="data" name="data" frameborder="0" noresize="true" scrolling="auto" hidefocus="true">
<noframes>


**login.asp:**
<!--CRN AND PASSWORD TABLE-->
    <table border="0" cellpadding="2" cellspacing=0>
        <tr>
            <td align="right" nowrap class="header5">Customer Registration Number:</td>
            <td valign="top" colspan="2"><font face="sans"><input type="TEXT" name="CorporateSignonCorpId" tabIndex=0 size="16" maxlength="19" AUTOCOMPLETE="OFF" onKeyPress="onFocus:microsoftKeyPress();" onFocus="javascript: strActiveField='CorporateSignonCorpId';"></font></td>
        </tr>
        <tr>
            <td align="right" class="header5">Password:</td>
            <td valign="top"><font face="sans"><input type="PASSWORD" name="CorporateSignonPassword" size="16" tabIndex=0 AUTOCOMPLETE="OFF" onKeyPress="javascript:microsoftKeyPress();" onFocus="javascript: strActiveField='CorporateSignonPassword';"></font></td>
        </tr>
        <tr>

How do I reference and set values for the attributes "CorporateSignonCorpId" and "CorporateSignonPassword" in C#?

ANY help would be appreciated!! thks Mick

Michael 1410
  • 73
  • 1
  • 9

1 Answers1

1

You can use webBrowser.Document.Window.Frames["main"].Document to get to the inner frame's document. The following works for me:

private async void MainForm_Load(object sender, EventArgs e)
{
    var tcs = new TaskCompletionSource<bool>();
    WebBrowserDocumentCompletedEventHandler handler = (s, arg) => 
        tcs.TrySetResult(true);

    this.webBrowser.DocumentCompleted += handler;
    this.webBrowser.Navigate("http://localhost:81/frameset.html");
    await tcs.Task;
    this.webBrowser.DocumentCompleted -= handler;

    var frameDocument = this.webBrowser.Document.Window.Frames["main"].Document;
    var element = frameDocument.GetElementById("CorporateSignonCorpId");
    MessageBox.Show(element.OuterHtml);
}
noseratio
  • 59,932
  • 34
  • 208
  • 486
  • Hi Noseratio - thanks for your suggestion but I get error: An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll – Michael 1410 Jan 02 '14 at 21:33
  • I have had success with this - is it achieving the same thing? wb.Url = wb.Document.Window.Frames[0].Url; wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wbFrame_DocumentCompleted); – Michael 1410 Jan 02 '14 at 21:34
  • I am then able to navigate and set the element values OK – Michael 1410 Jan 02 '14 at 21:39
  • 1
    @Michael1410, what line gives you that error? You really don't need to do another navigation to access the frame's content. `DocumentCompleted` can be fired **multiple times**, once per each frame and once for the whole page (we are interested in that one). The most reliable way to observe it for the whole page is to handle the page's DOM `onload` event. More info/samples [here](http://stackoverflow.com/a/18370524/1768303) and [here](http://stackoverflow.com/a/19063643/1768303). – noseratio Jan 03 '14 at 03:03