2016-07-24 60 views
0

我正在嘗試編寫一個程序,該程序使用WebBrowser控件在www.google.com中模擬簡單的瀏覽器搜索。我真的只是想模擬互聯網活動。使用WebBrowser控件模擬網絡搜索

我想出了使用循環發送一個號碼到谷歌搜索框,然後按下輸入的想法。

WebBrowser1.Document.GetElementById("q").SetAttribute("value", i)成功地將循環中的每個數字發送到谷歌搜索框,但下一行WebBrowser1.Document.GetElementById("btnK").InvokeMember("Click")不會啓動谷歌搜索按鈕。我沒有得到任何錯誤。

有沒有人有任何想法爲什麼WebBrowser1.Document.GetElementById("btnK").InvokeMember("Click")不起作用?

此外,我注意到,當我運行此代碼,然後啓動Internet Explorer時,代碼停止。有沒有人對此有任何想法?

非常感謝您的幫助!

問候

喬治

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 
+0

您是否嘗試過在循環之外放置'WebBrowser1.Document.GetElementById(「btnK」)。InvokeMember(「Click」)?因爲它會點擊100次點擊我相信 – Werdna

+0

嗨Werda,是的,我嘗試過,但仍然無法使用。當我將循環中的每個數字發送到Google搜索框時,我想按下谷歌搜索按鈕,因此在循環結束時,我將實際執行100次搜索。 – georgemackenzie

+0

好吧,等待火車ATM,儘快在電腦前,我會嘗試和GWT回你:) – Werdna

回答

0

好,工作過您給我們的例子。
試試這個。

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 


對不起,長的意見,我只是想幫助你理解的代碼在做什麼,你可能會或可能不會與可能的修復和或變通遇到沿着可能的錯誤。
祝你好運。