Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

vb.net - Webbrowser Click Button With No ID Or Name

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...