2013-08-19 14 views
0
public void PortalLogin() 
     { 
      string portalUrl = "URL"; 
      string portalEmail = "email"; 
      string portalPassword = "password"; 

      // Run when page finishes navigating 
      webBrowser2.DocumentCompleted += (s, e) => 
      { 
       HtmlElement head = webBrowser2.Document.GetElementsByTagName("head")[0]; 
       HtmlElement testScript = webBrowser2.Document.CreateElement("script"); 
       IHTMLScriptElement element = (IHTMLScriptElement)testScript.DomElement; 
       element.text = "function PortalLogin() { document.getElementById('username').value = '" + portalEmail + "'; document.getElementById('password').value = '" + portalPassword + "'; document.getElementById('credentials').submit(); }"; 
       head.AppendChild(testScript); 
       webBrowser2.Document.InvokeScript("PortalLogin"); 
      }; 

      // Navigate to the portal 
      webBrowser2.Navigate(portalUrl); 
      while (this.webBrowser2.ReadyState != WebBrowserReadyState.Complete) 
      { 
       Application.DoEvents(); 
       Thread.Sleep(100); 
      } 
     } 

我上面說的代碼段應該Navigate到特定的URL,然後在導航的完成,執行調用腳本登錄到網頁出現。現在,因爲整個PortalLogin()功能是while循環裏面,我必須包括:困惑於一些web瀏覽器功能

while (this.webBrowser2.ReadyState != WebBrowserReadyState.Complete) 
       { 
        Application.DoEvents(); 
        Thread.Sleep(100); 
       } 

段,以使while循環不只是中斷導航功能一噸。但是,代碼並不是很有效,當我用斷點逐步完成時,流程似乎關閉。我認爲部分問題是我不太瞭解while循環測試ReadState != Complete的工作原理。有人能啓發我嗎?

+0

只要刪除該代碼,它沒有任何用處。 –

回答

-2

更好地使用WebClient與Cookie或HttpWebRequest和HttpWebResponse。

+1

你是什麼意思? –