2011-02-16 128 views
3

我在一個小的VB.NET項目工作,該項目自動填充雅虎註冊頁面上的字段。有沒有辦法點擊「檢查」按鈕,看看輸入的ID是否正確?VB.NET WebBrowser點擊按鈕

類似於輸入的ID是否正確,然後繼續填寫字段,如果不是,請嘗試另一個ID並再次按「檢查」按鈕。

回答

5

WebBrowser控件可以讓你在網頁中訪問元素,你可以調用它們的方法,從而爲這個簡單的東西會點擊該按鈕:

webBrowser1.Document.All("yidHelperBtn").InvokeMember("click"); 
0

向您的應用程序添加一個計時器,間隔爲1000  毫秒。下面是代碼:

Dim CheckButton, yahooId As HtmlElement 
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _ 

把手WebBrowser1.DocumentCompleted

yahooId = WebBrowser1.Document.GetElementById("yahooid") 
    CheckButton = WebBrowser1.Document.GetElementById("yidHelperBtn") 

    yahooId.InnerText = "testID" 'Replace testID by the ID you want 

    Timer1.Start() 'Starts the timer: within 1000 ms (1 s). It will execute the Timer1_Tick sub. 

End Sub 

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 
    CheckButton.Focus() 'Give the check button the focus 
    SendKeys.Send("{ENTER}") 'Causes the validation of the check button 
    Timer1.Stop() 'Stops the timer 
End Sub 

我添加了一個計時器,因爲瀏覽器似乎並未驗證,而在WebBrowser1_DocumentCompleted方法Enter鍵。

使用此代碼,您可以知道您輸入的ID是否正確。它不完整,但這是一個好的開始,試着理解並適應您的需求。

+0

的確,正如約翰說,你也可以更換 'CheckButton.Focus() SendKeys.Send( 「{ENTER}」)' 由 WebBrowser1.Document.All( 「yidHelperBtn」)InvokeMember( 「點擊」) 它會做同樣的事情。但是,在文檔完成後您仍然需要等待1秒 – GianT971 2011-02-16 20:28:22