WebBrowser.DocumentComplete每次请求多次触发?

时间:2015-04-21 20:31:55

标签: c# winforms

我有一个包含以下代码的按钮

 webbrowser1.Navigate(testURL);

在我的事件处理程序

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();
    }
}

控制台消息打印出来四次!

发生了什么,我怎么能阻止它?

之前我查看了这个链接,并没有解决我的问题。

我尝试使用的网站没有任何框架或IFRAMES。我多次检查HTML。

下面的页面的HTML页面

<!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>

2 个答案:

答案 0 :(得分:0)

如果您的网页使用框架,则会多次触发DocumentCompleted。根据MSDN文档,事件只在没有帧的页面中被触发一次。在加载了多个帧的页面中,此事件将触发DownloadBegin事件触发的每个帧。

如果这会导致问题,您可以为onload添加一个事件处理程序,只会被触发一次:

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);
    });

答案 1 :(得分:0)

DocumentCompleted事件将针对加载页面中引用的每个框架和链接触发 - see this question以获取更多信息。

更好的选择是使用Navigated事件,或检查浏览器控件的ReadyState

if (browser.ReadyState != WebBrowserReadyState.Complete) {
    return;
}