使用sendkeys导航并等待webbrowser响应

时间:2015-01-31 16:29:12

标签: vb.net webbrowser-control sendkeys

是否可以打开网页,导航一次/单击按钮并第二次导航页面而无需使用三个按钮(每个按钮可以正常工作,有三个按钮)?

我试过睡觉(不起作用)和SendKeys.SendWait(也没用)。

以下是代码:

Dim browser As New WebBrowser
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        With browser
            browser.Name = "mybrowser"
            .Dock = DockStyle.Fill
            .Url = New Uri("http://pretraga2.apr.gov.rs/ObjedinjenePretrage/Search/Search")
            .Visible = True
        End With
        Me.Controls.Add(browser)
        browser.Focus()
        'browser.Focus()
        SendKeys.SendWait("{TAB}" & "{TAB}" & "08727988" & "{ENTER}") ' & "{TAB}" & "{ENTER}")
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        browser.Focus()
        SendKeys.Send("{TAB}" & "{TAB}" & "20084693" & "{ENTER}") ' & "{TAB}" & "{ENTER}")
        SendKeys.SendWait("{TAB}" & "{TAB}" & "{TAB}" & "{TAB}" & "{ENTER}")
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        browser.Focus()
        SendKeys.Send("{TAB}" & "{TAB}" & "{TAB}" & "{TAB}" & "{ENTER}")
    End Sub

1 个答案:

答案 0 :(得分:0)

是的我认为这是可能的,但您必须等到浏览器完成加载。你使用什么浏览器控件?我在这个例子中使用了SHDocVw.InternetExplorer:

 Public Sub browse(ByVal szURL As String, Optional ByVal bWaitTillDone As Boolean = True)

    Dim nTickStart As Long
    Dim nTickEnde As Long = 0

    Dim i As Integer = 0

    Do
        Try

            IE.Navigate(szURL)

            If bWaitTillDone Then

                nTickStart = System.Environment.TickCount

                While IE.Busy
                    fnSleepMS(15)

                    nTickEnde = System.Environment.TickCount
                    If nTickEnde - nTickStart > IEBrowser.nMaxWaitSeconds * 1000 Then
                        Throw New Exception("Browse hat zu lange gedauert!")
                    End If

                End While
            End If

            Exit Do

        Catch ex As Exception
            i += 1
            If i >= 5 Then
                Throw New Exception("IEBrowser.browse(): " & szURL & "--> " & ex.Message)
            Else
                LOG.add("IEBrowser.browse() - Versuch (" & i & ")" & ex.Message)
                fnSleep(1, LOG)
            End If
        End Try
    Loop

End Sub    

使用此浏览器控件,您可以读出所有ahref-Tags和Buttons并直接单击它们,这样您就可以构建序列。

希望我理解你,这有点帮助。但请告诉您在哪个环境中使用哪个浏览器控件(Framework + Visual Studio)