VB.NET Google搜索点击不能始终有效

时间:2017-12-06 21:06:05

标签: vb.net

我正在学习Webbrowser上与HtmlElements的自动交互。在此示例中,我在文本框中输入搜索字符串,然后单击搜索按钮。一旦完成,我会自动进行第二次搜索。

每次打开表单时,它都会正确导航到Google并将搜索字符串插入Google的搜索框...但按钮点击只能工作一半左右。每次打开表单时,它都不会点击按钮并执行搜索。为什么会发生这种情况?

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

0 个答案:

没有答案