2016-07-24 13 views
0

Webブラウザコントロールを使用してwww.google.comに単純なブラウザ検索をシミュレートするプログラムを作成しようとしています。私は実際にインターネット活動をシミュレートしたいだけです。WebBrowserコントロールを使用してWeb検索をシミュレートする

ループを使ってGoogle検索ボックスに数字を送信してからEnterキーを押すというアイデアが出てきました。

WebBrowser1.Document.GetElementById("q").SetAttribute("value", i)は、ループ内の各番号をgoogle検索ボックスに正常に送信しますが、次の行WebBrowser1.Document.GetElementById("btnK").InvokeMember("Click")はgoogle検索ボタンを開始しません。私は何の誤りもありません。

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検索ボックスに送信すると、Google検索ボタンを押すので、ループの最後までに100回の検索が行われます。 – georgemackenzie

+0

私は列車の気圧を待っています。私はコンピュータに向かってすぐに試してみるでしょう:) – Werdna

答えて

0

[OK]を、あなたが私たちに与えている例をオフに取り組んでいます。
これを試してください。

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 


長いコメントは申し訳ありませんが、私はちょうどあなたがコードをやって、あなたはしてもしなくてもよい可能な修正およびまたは回避策に沿って発生する可能性のあるエラーをされているものを理解する手助けしようとしています。
幸運。

関連する問題