[英]VB.NET WebBrowser click on button
我正在一個小型VB.NET項目中工作,該項目會自動填充Yahoo注冊頁面上的字段。 有沒有一種方法可以單擊“檢查”按鈕,查看輸入的ID是否正確?
如果輸入的ID正確,則請填寫該字段,如果沒有,請嘗試另一個ID,然后再次按“檢查”按鈕。
使用webbrowser控件,您可以訪問網頁中的元素,並且可以調用其上的方法,因此只需單擊按鈕即可進行以下操作:
webBrowser1.Document.All("yidHelperBtn").InvokeMember("click");
向您的應用程序添加一個計時器,間隔為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是否正確。 它並不完整,但這是一個好的開始,請嘗試理解並適應您的需求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.