单击webbrowser中的“确定”按钮

时间:2015-03-30 12:42:19

标签: vb.net

我在vb.net中使用gecko webbrowser导航到一个页面。

问题是当我打开那个页面时我仍然需要确定

Can't post images . Go through link

我想和invoke member一起去。 GetElemnetById但我不知道这个按钮

1 个答案:

答案 0 :(得分:0)

并不像你想象的那么容易,这就是pinvoke拯救的地方。

以下代码取自this discussion,并在此处提供,以防链接过时:

' test code, click OK button on dialog window...
Imports System.Runtime.InteropServices

Public Class Form1
    <DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="FindWindow")> _
    Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
    End Function
    <DllImport("user32.dll", EntryPoint:="SendMessage")> _
    Private Shared Function SendMessage(hwnd As IntPtr, wMsg As Integer, wParam As Integer, lParam As Integer) As IntPtr
    End Function
    Private Const BM_CLICK As Integer = &HF5
    Private Const WM_ACTIVATE As Integer = &H6
    Private Const WA_ACTIVE As Integer = 1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        ' find dialog window with titlebar text of "Message from webpage"
        Dim hwnd = FindWindow("#32770", "Message from webpage")
        If hwnd <> IntPtr.Zero Then ' dialog window found
            ' find button on dialog window: classname = "Button", text = "OK"
            Dim btn = FindWindowEx(hwnd, IntPtr.Zero, "Button", "OK")
            If btn <> IntPtr.Zero Then ' button found
                ' activate the button on dialog first or it may not acknowledge a click msg on first try
                SendMessage(btn, WM_ACTIVATE, WA_ACTIVE, 0)
                ' send button a click message
                SendMessage(btn, BM_CLICK, 0, 0)
            Else
                MsgBox("button not found!")
            End If
        Else
            MsgBox("window not found!")
        End If
    End Sub

End Class

您应该可以使用计时器来触发按钮单击。如果您不想在UI上使用按钮,只需将此代码放入自定义子代码中,然后在Timer中调用它。

归功于VB Forums成员@Edgemeal。