Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to use the WebBrowser in Visual Studio 2012 (Visual Basic) to click a button. I have done a lot of research, but all I can find is to click with an ID or a name.

Here is the HTML code.

<button type='submit' class="btn btn-default" style='float:right'>
    Login<i class="gicon-chevron-right"></i>

I am going to have the webbrowser hidden, & then when the user clicks Button1, it invokes the click on the webbrowser. Please help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to search for your element one by one. Try something like this:

For Each elem As HtmlElement In wb1.Document.GetElementsByTagName("button")
    ' Check the attributtes you want
    If elem.GetAttribute("class") = "btn btn-default" Then
        'Check even the text if you want
        If elem.InnerText = "Login" Then
            'Invoke your event
            elem.InvokeMember("click")
        End If
    End If
Next

This will search on all the button elements on the document. You could narrow the searching by looking inside some element that contains your button that has ID or something:

Dim myElem As HtmlElement = wb1.Document.GetElementById("myContainerId")
For Each elem As HtmlElement In myElem.GetElementsByTagName("button")
    '...
Next

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...