0

I am trying to write a program that simulates a simple browser search in www.google.com using the WebBrowser control. I'm really just wanting to simulate internet activity.

I came up with the idea of using a loop to send a number to the google search box and then pressing enter.

The line WebBrowser1.Document.GetElementById("q").SetAttribute("value", i) successfully sends each number in the loop to the google search box, but the next line WebBrowser1.Document.GetElementById("btnK").InvokeMember("Click") won't initiate the google search button. I don't get any errors.

Does anyone have any ideas why WebBrowser1.Document.GetElementById("btnK").InvokeMember("Click") doesn't work?

Also I've noticed that when I run this code and then launch Internet Explorer, the code stops. Does anyone have any ideas on this as well?

Most grateful for any help!

Regards

George

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Call LoadBrowser()

End Sub

Private Sub LoadBrowser()

    WebBrowser1.Navigate("http://www.google.com/")

End Sub 

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    ' Send search string 'i' to browser n times
    Dim i As Integer
    For i = 1 To 100
        ' Browser search 
        WebBrowser1.Document.GetElementById("q").SetAttribute("value", i)
        WebBrowser1.Document.GetElementById("btnK").InvokeMember("Click")
        ' Pause n seconds before next loop
        For x As Integer = 0 To 5 * 100 ' Pause for 5 seconds
            Threading.Thread.Sleep(10)
            Application.DoEvents()
        Next
    Next

End Sub
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
georgemackenzie
  • 171
  • 2
  • 4
  • 19
  • Have you tried putting 'WebBrowser1.Document.GetElementById("btnK").InvokeMember("Click")' outside of the loop? Because it will click 100 clicks i believe – user1234433222 Jul 24 '16 at 07:25
  • Hi Werda, yes I did try that and still doesn't work. When I send each number in the loop to the google search box I want to press the google search button, so by the end of the loop I will have in effect done 100 searches. – georgemackenzie Jul 24 '16 at 07:30
  • Ok im waiting for a train atm, as soon as im infront of a computer I'll try and gwt back to you :) – user1234433222 Jul 24 '16 at 07:31
  • Do you want it to only simulate the single number in the search box? – user1234433222 Jul 24 '16 at 07:50
  • 1
    I have two things to point out: **A)** Your use of the `Call` keyword is superfluous. You may use it if you want but it's unnecessary, so I'd change this: `Call LoadBrowser()` to this: `LoadBrowser()`. **B)** _**Don't**_ use `Application.DoEvents()` to keep your application responsive! Using it is a big mistake that many do. What it does is just allowing the form to repaint, but it may cause issues. Instead, use a `System.Windows.Forms.Timer` _or_ to better fit your current code: a thread. – Visual Vincent Jul 24 '16 at 08:07
  • It is very buggy code that will behave in inscrutable ways. This code belongs in an event handler for the DocumentCompleted event. – Hans Passant Jul 24 '16 at 08:09
  • Hi Hans! I was just going to link the OP to your answer: [**Why using Application.DoEvents is bad in most cases**](http://stackoverflow.com/a/5183623/3740093) – Visual Vincent Jul 24 '16 at 08:14
  • Yeah definitely odd, but never the less lets help him out :) – user1234433222 Jul 24 '16 at 08:14
  • Thankyou to everyone for their comments. I'm still transitioning from VB to VB.NET and much more to learn. I will swap out the DoEvent for a Timer control. Werdna, I tried your suggestion of 'sbds' and that didn't work so I'll go back to SendKeys to press the search button. – georgemackenzie Jul 24 '16 at 08:37

1 Answers1

0

Ok, working off the example you have given us.
Try this.

Imports WindowsInput '' <---------
''' <summary>
''' This program will run quick slow, would be best to use another thread for it, I'll leave that to you.
''' </summary>

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    WebBrowser1.Navigate("www.google.com.au")
End Sub
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim SIM = New InputSimulator() ''Use NuGet package manager and search for Input Simulator at Import it at the top     
    For i = 0 To 100

        WebBrowser1.Document.GetElementById("lst-ib").InvokeMember("Click") ''this will click on googles searchbox
        WebBrowser1.Document.GetElementById("q").InnerText = i
        Await Task.Delay(500) ''This will delay your code so you can see what is in the searchbox, I prefer this over Thread.Sleep, each to their own I guess.
        WebBrowser1.Document.GetElementById("q").InvokeMember("Click") ''This will click on googles search box to get the delete simulator ready.
        SIM.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.DELETE) '' This will delete whatever is in the searchbox since your original code was doing this and you didn't complain about it
    Next
    WebBrowser1.Document.GetElementById("lst-ib").InvokeMember("Click") ''ONCE the loop as completed it will click on googles search box
    SIM.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.RETURN) ''  Another keypress for the enter key (MAY NOT WORK DEPENDING IF YOU HAVE CLICK OFF THE WEBBROWSER CONTROL) maybe you can tab back to it?
    'WebBrowser1.Document.GetElementById("sblsbb").InvokeMember("Click") ''if the above line doesn't work
End Sub
End Class


Sorry for the long comments, I'm just trying to help you understand what the code is doing and the possible errors you may or may not encounter along with possible fixes and or work arounds.
Good luck.

user1234433222
  • 976
  • 2
  • 10
  • 21