getelement by class name for clicking

Viewed 5265

I am creating a windows form in VB.NET with web browser control, but not able to click on the below code.

<input type="Submit" class="btn btnSearch bold include_WidthButton" value="Search">

I can click when it is get element by ID, but not by classname. Please help me so much of googling didn't help me.

This is what I tried:

For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("btn btnSearch bold include_WidthButton")
    If Element.OuterHtml.Contains("btn btnSearch bold include_WidthButton") Then
        Element.InvokeMember("click")
    End If
Exit For
2 Answers

This is my solution:

Public Sub clickButton(ByVal web As WebBrowser, ByVal tagname As String, ByVal attr As String, ByVal contains As String, ByVal sleep As Integer)
    Try
        If (web.Document IsNot Nothing) Then
            With web.Document
                For Each Elem As HtmlElement In .GetElementsByTagName(tagname)
                    If (Elem.GetAttribute(attr).Contains(contains)) Then

                        Elem.InvokeMember("Click")
                        Thread.Sleep(sleep)
                        Return
                    End If
                Next
            End With
        End If
    Catch ex As Exception
        Show_Error(MODULE_NAME, "clickButton")
    End Try
End Sub

use:

clickButton(WebBrowser1, "button", "classname", "btn btnSearch bold include_WidthButton", 2000)
Related