1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Trying to click a button in webbrowser

Discussion in 'Visual Basic .NET' started by jas92, Feb 8, 2014.

  1. jas92

    jas92 Newbie

    Joined:
    Jan 20, 2014
    Messages:
    15
    Likes Received:
    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 :)
     
  2. ttrox

    ttrox Regular Member

    Joined:
    Jun 28, 2013
    Messages:
    217
    Likes Received:
    75
    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?
     
  3. KirtiD

    KirtiD Newbie

    Joined:
    Jun 13, 2013
    Messages:
    49
    Likes Received:
    15
    Location:
    Beed
    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
     
  4. jas92

    jas92 Newbie

    Joined:
    Jan 20, 2014
    Messages:
    15
    Likes Received:
    0
    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






    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: Feb 9, 2014
  5. KirtiD

    KirtiD Newbie

    Joined:
    Jun 13, 2013
    Messages:
    49
    Likes Received:
    15
    Location:
    Beed
    hi

    I already mentioned wc is name of web browser control pls read code again
     
  6. manchesterfan

    manchesterfan Junior Member

    Joined:
    Jun 19, 2013
    Messages:
    115
    Likes Received:
    12
    Location:
    Manchester (duh!)
    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/
     
  7. zohar

    zohar Newbie

    Joined:
    Jun 24, 2014
    Messages:
    44
    Likes Received:
    5
    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.
     
    • Thanks Thanks x 1
  8. Conor

    Conor Jr. VIP Jr. VIP

    Joined:
    Nov 7, 2012
    Messages:
    3,358
    Likes Received:
    5,419
    Gender:
    Male
    Location:
    South Africa
    Home Page:
    Jquery would be the easiest, if you can use it.

    Code:
    jQuery(document).ready(function($) {
    $( ".loginBnt" ).click()
    });
    
     
    Last edited: Oct 23, 2014
  9. zohar

    zohar Newbie

    Joined:
    Jun 24, 2014
    Messages:
    44
    Likes Received:
    5
    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: Oct 23, 2014
  10. MarketingFist

    MarketingFist Junior Member

    Joined:
    Jul 4, 2014
    Messages:
    156
    Likes Received:
    39
    Occupation:
    Internet Marketing
    Location:
    Novi Sad
    Home Page:
    Code:
    For Each webpageelement As HtmlElement In allelements
    
                If webpageelement.GetAttribute("value") = "Label from button" Then
    
                    webpageelement.InvokeMember("click")
    
                End If
     
  11. zohar

    zohar Newbie

    Joined:
    Jun 24, 2014
    Messages:
    44
    Likes Received:
    5
    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.
     
  12. saturated

    saturated Newbie

    Joined:
    Nov 8, 2013
    Messages:
    4
    Likes Received:
    2
    Replace "log inBnt" with "loginBnt"
    Hope this can help you! Thanks :)
     
    Last edited: Dec 22, 2014
  13. nickthecoder12

    nickthecoder12 Junior Member

    Joined:
    Jul 4, 2014
    Messages:
    134
    Likes Received:
    8
    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.