1

I am learning about automated interactions with HtmlElements on a Webbrowser. In this example, I am entering a search string into the textbox and then clicking the search button. Once that goes through, I automate a second search.

Each time I open the form it correctly navigates to Google and inserts the search string into the Google's search box... but the button click only works about half the time. Every other time I open the form it never clicks the button and performs the search. Why could this be happening?

Imports System.Text.RegularExpressions
Public Class frmGoogleTest

Private Sub webGoogleTest_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles webGoogleTest.DocumentCompleted

    'When Google is loaded, perform the first search
    If Regex.IsMatch(webGoogleTest.Url.AbsoluteUri, Regex.Escape("https://www.google.com/?gws_rd=ssl"), RegexOptions.IgnoreCase) Then
        webGoogleTest.Document.All.GetElementsByName("q")(0).SetAttribute("value", "first search")
        webGoogleTest.Document.All("btnK").InvokeMember("click")

    'Second search
    ElseIf Regex.IsMatch(webGoogleTest.Url.AbsoluteUri, Regex.Escape("&q=first+search"), RegexOptions.IgnoreCase) Then
        webGoogleTest.Document.All.GetElementsByName("q")(0).SetAttribute("value", "second search")
        webGoogleTest.Document.All("btnK").InvokeMember("click")

    End If
End Sub

Private Sub frmGoogleTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Go to Google when the form opens
    webGoogleTest.Navigate("http://www.google.com")
End Sub
End Class
  • Why not just use google's `q` URL get parameter for the searches and avoid having to click buttons alltogether? The very part of the URL that you're stripping out can be a useful tool like `webGoogleTest.Navigate("http://www.google.com/search?q=" & firstSearch)` and then later `webGoogleTest.Navigate("http://www.google.com/search?q=" & secondSearch)`. Clicking buttons and setting inputs' text can be a bit finicky sometimes, if you can skip that and just use the URL, then why bother with the page elements? – soohoonigan Dec 06 '17 at 21:34
  • @soohoonigan: He said he is learning to automate "interactions with HtmlElements", he is using Google just as a sample. – Sunil Dec 07 '17 at 03:50
  • Oh, I see...in that case, I think your problem may be with relying solely on the DocumentCompleted event. It's easy to assume that this event means everything is done loading, but unfortunately that's not always true. That event may fire multiple times per page, so if your navigation logic attempts to run before the page is really done loading, nothing will happen since the URL won't be filled. Good info on that in [this SO answer](https://stackoverflow.com/a/3239313/6664878). If you check the frame count and/or url before allowing your regex to run, that may solve your problem – soohoonigan Dec 07 '17 at 21:37
  • I tried the suggestions you linked and am still having the same problem. I even tried adding a delay with Thread.Sleep() to really make sure everything's loaded and it's still inconsistent. – Roger Leroy Dec 14 '17 at 20:04
  • Putting Application.DoEvents() after setting the value of Google's search box but before clicking the button seems to have helped and it works consistently now, but I'm not sure why. I've only ever used Application.DoEvents() to update the state of a form and its controls when executing code on the same thread freezes it. – Roger Leroy Dec 14 '17 at 20:13

0 Answers0