Click Button Just By Value Of It

Posts 1–2 of 2 · Page 1 of 1
Click Button Just By Value Of It
What exactly does this code do?

Code:
Private Function GetBtnByValue(ByVal value As String) As HtmlElement
        For Each d As HtmlElement In Webbrowser1.Document.GetElementsByTagName("input")
            If (d.GetAttribute("type").ToLower = asd.Text Or d.GetAttribute("type").ToLower = "submit") Then
                If (d.GetAttribute("value") = value) Then
                    Return d
                    Exit For
                End If
            End If
        Next
    End Function
    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        GetBtnByValue("go").InvokeMember("Click")
    End Sub


It is similar to GetElementByID

You can call this function whenever you want to click a web-element (using DOM) by Value

Whats a Value, A value can be many things, but in most cases it is the same as the text on the button, which makes it easier to locate (sometimes values can be different, like in combo-boxes , something connected to a database, or similar)

So using that function you can easily click , lets say the Go Advanced Button on this forum by using

Code:
GetBtnByValue("Go Advanced").InvokeMember("Click")
I like it !

BreakDown

Code:
Private Function GetBtnByValue(ByVal value As String) As HtmlElement
        For Each d As HtmlElement In Webbrowser1.Document.GetElementsByTagName("input")
            If (d.GetAttribute("type").ToLower = asd.Text Or d.GetAttribute("type").ToLower = "submit") Then
                If (d.GetAttribute("value") = value) Then
                    Return d
                    Exit For
                End If
            End If
        Next
    End Function
This part of the code creates the function Get BtnByValue

Code:
    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        GetBtnByValue("go").InvokeMember("Click")
    End Sub
This part of your example says if you click on button one in the toolbar then click the button in the webbrowser which has the value "go"

You can use this code for alot of reasons, Some buttons don't always have ID's , But will have values.

Another Example would be

GetBtnByValue ("Click Me").InvokeMember("Sumbit")



Posts 1–2 of 2 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Talk with us