-1

I have a button that has the following code

 webbrowser1.Navigate(testURL);

In my event handler

private void webbrowser1_Browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
{
    if (e.Url.Equals(psyncBrowser.Url))
    {
        Console.WriteLine("Now it is really done");
        MessageBox.Show("Loaded " + psyncBrowser.Url);
        webbrowser1.Stop();
    }
}

The console message is printed out four times!

What's happening, and how can I stop this?

I looked at that link before and it did not address my concern.

The site I am trying to use does not have any FRAMES or IFRAMES. I checked the HTML several times.

FUll HTML of Page below

<!DOCTYPE html>

<html>
<head>
  <title>Hitachi ID Identity and Access Management Suite: Verify password</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="expires" content="-1">
  <meta http-equiv="cache-control" content="private,no-cache,no-store,maxage=0,s-maxage=0,must-revalidate,proxy-revalidate,no-transform">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <script type="text/javascript">
// <![CDATA[
  // Array of fields to process and attempt to focus on page load
  document.focusFields = [ "_MYPW", null ];
  // ]]>
  </script>
  <link rel="stylesheet" type="text/css" media="all" href="docs/default/css/style.css?3242224743"><!--[if lte IE 8]><link rel="stylesheet" type="text/css" media="all" href="docs/default/css/style-ie8.css?3242224743" /><![endif]-->
  <!--[if lte IE 7]><link rel="stylesheet" type="text/css" media="all" href="docs/default/css/style-ie7.css?3242224743" /><![endif]-->
  <!--[if lte IE 6]><link rel="stylesheet" type="text/css" media="all" href="docs/default/css/style-ie6.css?3242224743" /><![endif]-->

  <script src="docs/default/js/en-US/lang.js?3242224743" type="text/javascript">
</script>
  <script id="C_AUTHCHAIN_LOGIN:default:en-US:" src="docs/default/js/scripts.js?3242224743" type="text/javascript">
</script>
</head>

<body class="default noSide">
  <a name="top" id="top"></a>

  <form name="theform" method="post" action="psf.exe" autocomplete="off" id="theform">
    <div id="container">
      <span id="topMargin"><input id="TRANSACTION" name="TRANSACTION" type="hidden" value="C_AUTHCHAIN_LOGIN"> <!-- 81A84EBD-2CE5-4794-8341-E1828711FFBC -->
      <input id="SESSDATA" name="SESSDATA" type="hidden" value="{AES}EIDgCtlm1WIHTuwgStamCgi1cvi98CgOIhXUvVy7N2o3jDv6ce5qxqTFLdgzyU+QYBoq5w064mzb174I2rLVeq1HT8Z+hzi78gSMPbDzX0CBy0/GVTAfFsXk4OR6hc/e"> <input id="SESSLENGTH" name="SESSLENGTH" type="hidden" value="600"> <input id="SKIN" name="SKIN" type="hidden" value="default"> <input id="LANG" name="LANG" type="hidden" value="en-US"> <input id="JS" name="JS" type="hidden" value="false"> <input disabled="disabled" id="currentDateTime" type="hidden" value="2015-04-22 08:05:08 "> <input alt="" class="hideIfJs" id="DEFAULT_PAGE_ACTION" name="DEFAULT_UNUSED_BUTTON.x" src="docs/pics/spacer.gif" style="border: none; height: 1px; width: 1px;" tabindex="1000" title="" type="submit">   <span id="skipNav"><a href="#topMenu">skip to main navigation</a> | <a href="#sideMenu">skip to side navigation</a> | <a href="#content">skip to main content</a></span> <!-- end #skipNav --></span><!-- end #topMargin -->

      <div id="headerContainer">
        <div id="header">
          <div id="companyLogo">
            <img src="docs/pics/company_logo.png" width="103" height="17" alt="Hitachi">

            <div class="line"></div>
          </div>

          <div id="topButtons">
            <ul>
              <li><input class="submit submitIcon back" name="SUBMIT-BACK.x" title="Back" type="submit" value=""></li>

              <li class="inactive"><input class="submit inactiveIcon home" disabled="disabled" name="SUBMIT-HOME.x" title="Main menu (Disabled)" type="submit" value=""></li>

              <li class="inactive"><input class="submit inactiveIcon refresh" disabled="disabled" name="SUBMIT-REFRESH.x" title="Refresh (Disabled)" type="submit" value=""></li>

              <li><input class="submit submitIcon logout" name="SUBMIT-LOGOUT.x" title="Logout" type="submit" value=""></li>
            </ul>
          </div><!-- end #topButtons -->

          <div id="topMenuBar">
            <div class="clearFloat"></div>
          </div><!-- end #topMenuBar -->
        </div><!-- end #header -->

        <div id="userInfo">
          <img src="docs/pics/icon_idcard.png" alt="ID" width="16" height="12"> ID: username   <img src="docs/pics/icon_user.png" alt="User" width="10" height="14"> Name: Person Name
        </div>
      </div><!-- end #headerContainer -->

      <div id="contentScroll">
        <table cellspacing="0" id="colWrap">
          <tbody>
            <tr>
              <td id="menuCol">
                <div id="menuContent"></div><!-- end #menuContent -->
              </td><!-- end #menuCol -->

              <td id="mainCol">
                <div id="content">
                  <h1>Verify password <span class="selectedObj"></span></h1><br>
                  <input type="hidden" name="LANG" value="en-US"><input type="hidden" name="SKIN" value="default"> <span class="bold">Enter password:</span>

                  <table cellspacing="0" class="border">
                    <tbody>
                      <tr>
                        <td class="prompt" width="50%"><span>Enter password for AD:</span></td>

                        <td width="50%"><input class="password text" maxlength="250" name="_MYPW" type="password"></td>
                      </tr>

                      <tr class="foot">
                        <td colspan="2"><input class="submit" name="SUBMIT-VERIFY.x" type="submit" value="Verify password"></td>
                      </tr>
                    </tbody>
                  </table><input type="hidden" name="SETTABLEFIELDSDB" value=""> <input type="hidden" name="SETTABLEFIELDSCOOKIE" value="">
                </div><!-- end #content -->
              </td><!-- end #mainCol -->
            </tr>
          </tbody>
        </table><!-- end #colWrap -->
      </div><!-- end #contentScroll -->

      <div id="footer">
        <div id="footerCompany">
          Company Name
        </div>

        <div id="footerVersion">
          <b>Hitachi ID Identity and Access Management Suite</b> v8.2.7.60749
        </div>

        <div id="footerCopyright">
          <div>
             <sup> </sup>© 2014 <a id="footerLink" href="http://Hitachi-ID.com" rel="external" target="_blank">Hitachi ID Systems, Inc.</a>
          </div>
        </div>
      </div>
    </div><!-- end #container -->
  </form>
</body>
</html>
software is fun
  • 7,286
  • 18
  • 71
  • 129
  • @CodeCaster: It appears that the problem in the question you referenced was caused by multiple frames in the page being loaded. Since the page being loaded in this question's example doesn't have frames, how can the answer to the other question adequately resolve this question? – Trevor Apr 23 '15 at 17:41
  • 1
    @threed that answer there is incomplete, and OP's case is not reproducible. The event also fires for HTTP requests fired from Javascript, as well as for redirects. OP can apply the "easy way" fix from the duplicate - unless there's a redirect taking place. – CodeCaster Apr 23 '15 at 17:50

2 Answers2

0

DocumentCompleted can get fired multiple times if your page uses frames. According to the MSDN documentation the event only gets fired once in pages with no frames. In pages where multiple frames are loaded, this event fires for each frame where the DownloadBegin event has fired.

If this causes a problem you could add an event handler to onload which only get fired once:

this.webBrowser1.Document.Window.AttachEventHandler("onload", delegate
    {
        // Defer this to make sure all possible onload event handlers got fired
        System.Threading.SynchronizationContext.Current.Post(delegate 
        {
            // try webBrowser1.Document.GetElementById("id") here
            MessageBox.Show("window.onload was fired, can access DOM!");
        }, null);
    });
Alex
  • 21,273
  • 10
  • 61
  • 73
0

The DocumentCompleted event will fire for each frame and link referenced in the page loaded - see this question for more info.

A better alternative would be to use the Navigated event, or check the ReadyState of the browser control.

if (browser.ReadyState != WebBrowserReadyState.Complete) {
    return;
}
Community
  • 1
  • 1
Gareth Flowers
  • 1,513
  • 12
  • 23