Ionut Ungureanu
Ionut Ungureanu

Reputation: 380

VB.NET WebBrowser click on button

I am working at a small VB.NET project which autofill the fields on the Yahoo register page. Is there a way to click on "Check" button and see if the entered ID is OK or not?

Something like if the entered ID is OK then proceed further with filling the field, if not, try another ID and press "Check" button again.

Upvotes: 4

Views: 39109

Answers (2)

GianT971
GianT971

Reputation: 4523

Add a timer to your application, with an interval of 1000 ms. Here is the code:

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

Handles 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

I added a timer because the browser doesn't seem to validate the Enter key while in the WebBrowser1_DocumentCompleted method.

With this code, you can know if the id you entered is OK or not. It is not complete, but it's a good beginning, try to understand and adapt it for your needs.

Upvotes: 0

John Koerner
John Koerner

Reputation: 38087

The webbrowser control lets you access elements within the webpage and you can invoke methods on them, so something as simple as this will click the button:

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

Upvotes: 7

Related Questions