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

限于喜欢 提交于 2020-12-13 03:21:21

问题


How can I click this SUBMIT button using a WebBrowser control?

I've tried with:

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

but it doesn't return anything.


回答1:


Here's a sample procedure to perform a WebForm LogIn using a WebBrowser control.

Note: I suggest to activate the WebBrowser Emulation Advanced Features beforehand, in case its need. Read a description here (see the WebBrowserAdvancedFetures class):
(The FEATURE_GPU_RENDERING Key may not exist, so it may be necessary to create it first)

  • Subscribe to the WebBrowser.DocumentCompleted event before navigating to an Address
    • As describe in How to get an HtmlElement value inside Frames/IFrames?, the Document may be actually composed of more than one Frame/IFrame (the latter, quite common). If that's the case, you need to handle the DocumentCompleted more than once, since each Frame/IFrame has its own Document
  • When the event is raised, check whether the WebBrowser.ReadyState is WebBrowserReadyState.Complete: return if its not (we don't want to handle partial documents).
  • When the current Document is complete find a Form with a specific ID or class name: that's the Login Form we want to fill in.
  • When the Form is found, parse its Document and to select the INPUT elements that require a value.
  • If all elements are found and their values has been set, find the SUBMIT Button and call its InvokeMember() method, specifying the click handler to complete the procedure and activate the Form POST method.
  • When the Button is clicked, the WebBrowser is redirected to a landing page, so we remove the Handler of the DocumentCompleted event: we're done here, handling this event is no longer need.

► Set [WebBrowser].ScriptErrorsSuppressed = True in the Form's Designer.

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


来源:https://stackoverflow.com/questions/64655652/how-to-fill-a-webform-and-click-the-submit-button-with-a-webbrowser-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!