Trying to click a button in webbrowser

jas92

Newbie
Joined
Jan 20, 2014
Messages
15
Reaction score
0
Hello, how would I click a button if there's no ID for it? This is the button I'm trying to click
Code:
<button class="loginBnt" type="submit"></button>
how would I put it into something like this
Code:
WebBrowser1.Document.GetElementById("").InvokeMember("click")
I've tried searching on Google but I can't find anything. Is this possible? Thanks for the help :)
 
I don't know anything about visual basic, but I figure there might be a function for GetElementByClass? Get the elements with class "loginBnt", grab the first one (the only on the page?) and click it.

Right?
 
Hi

here is trick , hope this will work, If you still face issue pls share complete html of login button
'wc is web browser control'
Dim doc As HtmlDocument = wc.Document
Dim link As HtmlElement
Dim links As HtmlElementCollection = wc.Document.All


For Each link In links
If link.GetAttribute("href").Contains("/login/") Or link.GetAttribute("href").Contains("/login.php?return=") Then
link.InvokeMember("click")
End If
Next
 
I don't know anything about visual basic, but I figure there might be a function for GetElementByClass? Get the elements with class "loginBnt", grab the first one (the only on the page?) and click it.

Right?
I'm new to Visual Basic but I'm determined to get this working lol. For some reason "GetElementByClass" don't work, I get this error
Code:
Error    1    'GetElementByClass' is not a member of 'System.Windows.Forms.HtmlDocument'.
Thanks for the reply mate






Hi

here is trick , hope this will work, If you still face issue pls share complete html of login button
'wc is web browser control'
Dim doc As HtmlDocument = wc.Document
Dim link As HtmlElement
Dim links As HtmlElementCollection = wc.Document.All


For Each link In links
If link.GetAttribute("href").Contains("/login/") Or link.GetAttribute("href").Contains("/login.php?return=") Then
link.InvokeMember("click")
End If
Next
I'll give this a try now mate and report back :)

EDIT: It's coming up with this error mate:
Code:
Error    1    'wc' is not declared. It may be inaccessible due to its protection level.
For some reason I can't post the source page for the login yet.. can I add you on Skype or anything mate?
 
Last edited:
hi

I already mentioned wc is name of web browser control pls read code again
 
Code:
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim allelements As HtmlElementCollection = WebBrowser1.Document.All

        For Each webpageelement As HtmlElement In allelements

            If webpageelement.GetAttribute("value") = "Log In" Then

                webpageelement.InvokeMember("click")

            End If

        Next
    End Sub

More can be wathced here. http://howtostartprogramming.com/vb-net/vb-net-tutorial-49-click-button-without-id/
 
If there is no id, try className. If there is no className either, then you are stuck with counting the element's position inside the HTML DOM. Good luck.
 
Jquery would be the easiest, if you can use it.

Code:
jQuery(document).ready(function($) {
$( ".loginBnt" ).click()
});
 
Last edited:
Try

document.querySelectorAll("button");

and then get the element index.

Be warned, this works only from IE8 'n up. This means, if you install your VB app on a < XP SP3 computer it will most likely fail. If my memory serves me correctly, IE7 to IE8 only support <element>.querySelector but not querySelectorAll . The mind of a schizophrenic is more peaceful then a browser war, so always read MSDN for info. Good luck.
 
Last edited:
Code:
For Each webpageelement As HtmlElement In allelements

            If webpageelement.GetAttribute("value") = "Label from button" Then

                webpageelement.InvokeMember("click")

            End If
 
Best practice IMHO is to inject javascript in your document and let the js code do all the DOM manipulation, since .NET HTML* classes *may* differ between IE versions.. it's a real mess.
 
Dim theElementCollection As System.Windows.Forms.HtmlElementCollection
theElementCollection = WebBrowser1.Document.GetElementsByTagName("button")
For Each curElement As HtmlElement In theElementCollection
If curElement.GetAttribute("className").Contains("loginBnt") Then
curElement.InvokeMember("click")
Replace "log inBnt" with "loginBnt"
Hope this can help you! Thanks :)
 
Last edited:
You can go with Labeled text instead of ID. Lets say button has id login and the labeled text is text on it " Login" you can click on button by labeled text.
 
Back
Top