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

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

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top