如何使用 WebBrowser 控件填写 WebForm 并单击提交按钮?

How to fill a WebForm and click the submit Button with a WebBrowser control?

如何使用 WebBrowser 控件单击此 SUBMIT 按钮?

我试过:

For Each divSect As HtmlElement In WebBrowser1.Document.GetElementsByTagName("button")
    If divSect.OuterHtml.Contains("Accedi") Then
        For Each elem As HtmlElement In divSect.Children
            If elem.GetAttribute("type") = "button" Then
                elem.InvokeMember("click")
            End If
        Next
    End If
Next

但它 return 什么都没有。

下面是使用 WebBrowser 控件执行 WebForm 登录的示例过程。

注意:我建议预先激活 WebBrowser Emulation Advanced Features,以备不时之需。阅读(见WebBrowserAdvancedFeturesclass):
FEATURE_GPU_RENDERING 键可能不存在,因此可能需要先创建它)

  • 在导航到地址之前订阅 WebBrowser.DocumentCompleted 事件
    • 所述,Document实际上可能由多个Frame/IFrame组成(后者很常见)。如果是这种情况,您需要多次处理 DocumentCompleted,因为每个 Frame/IFrame 都有自己的 Document
  • 事件触发时,检查是否 WebBrowser.ReadyState is WebBrowserReadyState.Complete:如果不是 return(我们不想处理部分文档)。
  • 当前文档完成后,找到具有特定 ID 或 class 名称的表单:这就是我们要填写的登录表单。
  • 找到表单后,解析其文档并select INPUT 需要值的元素。
  • 如果找到所有元素并设置它们的值,找到 SUBMIT 按钮并调用其 InvokeMember() 方法,指定 click 处理程序以完成该过程并激活形式 POST 方法。
  • 当按钮被点击时,WebBrowser 被重定向到一个登录页面,所以我们删除了 DocumentCompleted 事件的处理程序:我们到这里就完成了,不再需要处理这个事件。

▶ 在表单设计器中设置 [WebBrowser].ScriptErrorsSuppressed = True

Private Sub btnNavigate_Click(sender As Object, e As EventArgs) Handles btnNavigate.Click
    AddHandler webBrowser1.DocumentCompleted, AddressOf Browser_DocumentCompleted
    webBrowser1.Navigate("https://SomeAddress.com")
End Sub

Private Sub Browser_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
    Dim browser = DirectCast(sender, WebBrowser)
    If browser.ReadyState <> WebBrowserReadyState.Complete Then Return

    ' Select a Form with a specific className
    Dim form = browser.Document.Forms.OfType(Of HtmlElement).FirstOrDefault(Function(frm) frm.GetAttribute("className").StartsWith("form-login"))

    ' Not found at this point, try later
    If form Is Nothing Then Return

    ' Select the element by ID or by className or whatever
    Dim userId = form.Document.GetElementById("[The UserId Input's ID]")
    ' Same for other input elements
    Dim userPwd = form.Document.GetElementById("[The Password Input's ID]")
    If userId Is Nothing OrElse userPwd Is Nothing Then Return

    ' Set the value of both Input elements. Note that a validation procedure 
    ' may require that you set both the Value and the InnerText
    userId.SetAttribute("value", "[The User LogIn ID]")
    'userId.InnerText = "[The User LogIn ID]"
    userPwd.SetAttribute("value", "[The User LogIn Password]")
    'userPwd.InnerText = "[The User LogIn Password]"

    ' Filter (fail-safe) the SUBMIT button by className, since - here - it has no ID
    Dim submit = browser.Document.GetElementsByTagName("button").OfType(Of HtmlElement).
        FirstOrDefault(Function(elm) elm.GetAttribute("type").
            Equals("submit") AndAlso elm.GetAttribute("className").Contains("bt-label"))

    ' The SUBMIT Button was found: click it. Also remove the handler: we're done here
    ' The WebBrowser is redirected to a landing page, this event is no longer needed
    If submit IsNot Nothing Then
        RemoveHandler browser.DocumentCompleted, AddressOf Browser_DocumentCompleted
        submit.InvokeMember("click")
    End If
End Sub