Wednesday, July 6, 2016

WebBrowser how to fire click event of input submit element

Given in a webpage there are several input elements and you want to fire the click event of an input element of type submit, here's how to do it using the Webbrowser control.
C#
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
HtmlElementCollection htmlElementCollection = new HtmlElementCollection();
if (WebBrowser1.Document != null) 
{
    htmlElementCollection = WebBrowser1.Document.GetElementsByTagName("input");
    if (htmlElementCollection != null && htmlElementCollection.Count > 0)
    {
        foreach (HtmlElement element in htmlElementCollection) 
        {
            if (element.GetAttribute("value").Equals("Submit")) 
            {
                element.InvokeMember("click");
            }
        }
    }
}

VB.NET
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
Dim htmlElementCollection As HtmlElementCollection
If (WebBrowser1.Document IsNot Nothing) Then
    With WebBrowser1.Document
        htmlElementCollection = .GetElementsByTagName("input")
        If (Not htmlElementCollection Is Nothing And htmlElementCollection.Count > 0) Then
            For Each element As HtmlElement In htmlElementCollection
                If (element.GetAttribute("value").Equals("Submit")) Then
                    element.InvokeMember("click")
                End If
            Next
        End If
    End With
End If

0 comments:

Post a Comment