Title says it all.
Printable View
Title says it all.
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()
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?
to sum up, if its a button on web page the easiest way to click will always be .getelementbyid
in that instance your button id is "gbqfba". If you need to sort via class, you will use the .getelementsbytagname()Code:<button name="btnK" class="gbqfba" id="gbqfba" aria-label="Google Search">
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)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
Hi, I decided to test the code today, and when testing with google accounts login, Where the button ID is "signIn" I put-
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:htmldoc.getelementbyid("signIn").click()
I'm not sure what I'm doing wrong. Thank-you for the quick replies though :)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
This works fine for me:
create a button and add this to the click event
on load navigate to the pageCode:htmldoc = AxWebBrowser1.Document
htmldoc.getElementById("signin").click()
now run the app, then add in credentials and then press the button. It works for me?Code:AxWebBrowser1.Navigate("https://accounts.google.com/Login?continue=http://history.google.com/history/&hl=en&authuser=0")
It works now :D Thank you so much.
Hey...could you please just help me with this one piece of code. :)
I have this button:
So what code would I use to click it?Code:<div class="btn3">Like</div>