[RESOLVED] Click Button in AxWebBrowser Control-VBForums
Results 1 to 8 of 8

Thread: [RESOLVED] Click Button in AxWebBrowser Control

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    40

    Resolved [RESOLVED] Click Button in AxWebBrowser Control

    Title says it all.

  2. #2
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,173

    Re: Click Button in AxWebBrowser Control

    I would use the mshtml object library in conjunction with the axwebbrowser to click buttons.

    you need to add your reference to the mshtml object library and declare it:
    imports mshtml

    And as a class-level dimension (if intended to use in different subs) to set the instance of the htmldoc to the web page:

    dim htmldoc as mshtml.htmldocument 'htmldoc is like a snapshot of the current page that you can then run commands on

    then in the sub you need to set the web page instance under htmldoc to detect the button and click it

    htmldoc = AxWebBrowserControl1.document


    htmldoc.getelementbyid("ButtonID").click()
    Last edited by jayinthe813; Mar 7th, 2013 at 10:47 PM.

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    40

    Re: Click Button in AxWebBrowser Control

    Thanks, I'll try it out tommorow and see if it works. Just one thing though, is there a way to click a button by knowing its class?

  4. #4
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,173

    Re: Click Button in AxWebBrowser Control

    to sum up, if its a button on web page the easiest way to click will always be .getelementbyid

    Code:
    <button name="btnK" class="gbqfba" id="gbqfba" aria-label="Google Search">
    in that instance your button id is "gbqfba". If you need to sort via class, you will use the .getelementsbytagname()

    Code:
            htmldoc = AxWebBrowser1.Document 'set to instance of current web page
    
            Dim classtags = htmldoc.getElementsByTagName("CLASS") 'create a list of all the html elements containing the class tag
    
            For Each item In classtags 'loop throuh each item
    
                If item.value = "BUTTON" Then 'create a parameter that uniquely identifies the button on your web page
    
                    item.invokemember("click") 'invoke a click on the element
    
                End If
    the tricky part there is understanding which item needs to use the invokemember in order to click. Notice its getelements and not ELEMENT. You are basically populating every item containing "Class" tag into a collection. You will need to look at the html to decide what unique indicator lies within the tag in order to narrow down which item is the button, such as an item type or a value "go" for example or else the program will not be able to identify which to click. If the item is within a tag, you can use firstchild.click (I dont really understand that part of it)
    Last edited by jayinthe813; Mar 7th, 2013 at 10:43 PM.

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    40

    Re: Click Button in AxWebBrowser Control

    Hi, I decided to test the code today, and when testing with google accounts login, Where the button ID is "signIn" I put-

    Code:
    htmldoc.getelementbyid("signIn").click()
    This didn't click the button though...do you know what I am doing wrong? I even tried the class method. No errors, but no button click either-

    Code:
          Private Sub AxWebBrowser1_DocumentComplete(sender As Object, e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent)
            TextBox1.Text = AxWebBrowser1.LocationURL
            If TextBox1.Text.Contains("accounts.google.com") Then
                htmldoc = AxWebBrowser1.Document 'set to instance of current web page
    
                Dim classtags = htmldoc.getElementsByTagName("g-button g-button-submit") 'create a list of all the html elements containing the class tag
    
                For Each item In classtags 'loop throuh each item
                    If item.value = "Sign in" Then 'create a parameter that uniquely identifies the button on your web page
                        item.invokemember("click") 'invoke a click on the element
                    End If
                Next
            End If
        End Sub
    I'm not sure what I'm doing wrong. Thank-you for the quick replies though
    Last edited by johnwillmikul; Mar 7th, 2013 at 11:23 PM.

  6. #6
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,173

    Re: Click Button in AxWebBrowser Control

    This works fine for me:


    create a button and add this to the click event

    Code:
            htmldoc = AxWebBrowser1.Document
    
            htmldoc.getElementById("signin").click()
    on load navigate to the page

    Code:
     AxWebBrowser1.Navigate("https://accounts.google.com/Login?continue=http://history.google.com/history/&hl=en&authuser=0")
    now run the app, then add in credentials and then press the button. It works for me?

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    40

    Resolved Re: Click Button in AxWebBrowser Control

    It works now Thank you so much.

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    40

    Re: Click Button in AxWebBrowser Control

    Hey...could you please just help me with this one piece of code.

    I have this button:

    Code:
    <div class="btn3">Like</div>
    So what code would I use to click it?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.