如何处理在 VB.NET 中的 Web 浏览器对象中按下的 HTML 按钮?

How do I handle an HTML button being pressed in a web browser object in VB.NET?

VB.NET菜鸟来了,

有什么方法可以处理在 Web 浏览器控件中单击的 HTML 对象吗?我设法获得的最接近的是浏览器的鼠标按下事件处理程序,它写入鼠标的位置,但我无法获得有关 html 对象被单击的信息。

Private Sub myWB_mouseDown(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
    If e.MouseButtonsPressed = Windows.Forms.MouseButtons.Left Then
        'mousedown event
        Debug.WriteLine(e.ClientMousePosition)
    End If
End Sub

因为你有点击的坐标,你可以使用 HtmlDocument.GetElementFromPoint() 来获取点击点的元素。

然后您可以检查元素的 TagName property to determine the type of the element that was clicked, namely if it's a <button> or an <input> element. If the latter, you also need to check GetAttribute("type") 以确定输入元素的类型是否为 submit,这意味着它是一个按钮。

If myWB.Document IsNot Nothing AndAlso e.MouseButtonsPressed = Windows.Forms.MouseButtons.Left Then
    Dim ClickedElement As HtmlElement = myWB.Document.GetElementFromPoint(e.ClientMousePosition)

    If ClickedElement IsNot Nothing AndAlso _
        (ClickedElement.TagName.Equals("button", StringComparison.OrdinalIgnoreCase) OrElse _
         (ClickedElement.TagName.Equals("input", StringComparison.OrdinalIgnoreCase) AndAlso ClickedElement.GetAttribute("type").Equals("submit", StringComparison.OrdinalIgnoreCase))) Then

        'ClickedElement was either a <button> or an <input type="submit">. Do something...

    End If
End If