2015-04-14 49 views
0

我有一個關於重定向url的問題。我正在自動填充。填寫表格後,我提交表格並點擊按鈕。之後,它會重新顯示一個新頁面。我需要這個重定向網址。以編程方式填寫表單後的重定向url

作爲例子

webBrowser1.Navigate("myurl.com") 

     For I As Integer = 0 To 500 
      If webBrowser1.ReadyState = WebBrowserReadyState.Complete Then Exit For 
      Threading.Thread.Sleep(1) 
      Application.DoEvents() 
     Next 

     Dim elementLogin = webBrowser1.Document.GetElementById("login_identifier") 
     If Not IsNothing(elementLogin) Then 
      webBrowser1.Document.GetElementById("login_identifier").SetAttribute("Value", "myemail") 
     End If 
     Dim elementPassword = webBrowser1.Document.GetElementById("login_password") 
     If Not IsNothing(elementLogin) Then 
      webBrowser1.Document.GetElementById("login_password").SetAttribute("Value", "mypassword") 
     End If 
     webBrowser1.Document.GetElementById("login_btn").Enabled = True 
     webBrowser1.Document.GetElementById("login_btn").InvokeMember("click") 

我需要點擊該按鈕後重定向的URL。當它點擊登錄按鈕後重定向到新頁面。如果可能,請幫我解決此問題 謝謝

回答

0

您可以使用WebBrowser.URL來獲取要導航到的網頁。但是,如果您在導航開始前嘗試獲取新的URL(這意味着您將獲得當前的 URL),則這可能會導致問題。

相反,嘗試通過按鈕ID匹配WebBrowser.Document的所有元素循環,並最終獲得按鈕的href

Dim NextURL As String() = "" 
For Each ele As HtmlElement In webBrowser1.Document.All 
    If ele.GetAttribute("id") = "login_btn" then 
     NextURL = ele.GetAttribute("href") 
     Exit For 
    End If 
Next 

這會觸發「無」異常和一次爲多元素將工作。另外,對你的一個單獨的按鈕,也許更好,使用方法:

Dim NextURL As String = webBrowser1.Document.GetElementById("login_btn").GetAttribute("href") 

編輯:隨着按鈕的href標籤似乎空,不如訂閱WebBrowser.DocumentCompleted事件處理重定向後得到新的網絡ADRESS通過WebBrowser.URL。祝你好運!

+0

感謝您的回覆。其實ele.GetAttribute(「href」)是空的。 –

+0

它是空的還是「#」?這將表明有JavaScript或其他參與。 –

+0

它只是空的「」 –