问题
How do I write the code to click in that button "Catálogo"?
Button and HTML Code Image:
My code so far:
'This will load a webpage in IE
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
'Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
URL = "https://www.wix.com/my-account/sites/0761466b-a270-4ab2-a3b3-e7498df11329/app/1380b703-ce81-ff05-f115-39571d94dfcd?referralInfo=DA_Wix%20Stores"
'Navigate to URL
IE.navigate URL
' Statusbar let's user know website is loading
Application.StatusBar = URL & " is loading. Please wait..."
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.readyState = 4: DoEvents: Loop 'Do While
Do Until IE.readyState = 4: DoEvents: Loop 'Do Until
'Webpage Loaded
Application.StatusBar = URL & " Loaded"
'Unload IE
Set objElement = Nothing
Set objCollection = Nothing
'Click on desired button
With IE.document
Set elems = .getElementsByTagName(" ")
elems.Click
End With
The tag name is blank because I tried lots of things and none of them worked.
回答1:
The relevant element is not actually a button or an <input type=button/>, but rather an a element, or anchor element.
Keep in mind that getElementsByTagName will return all the a elements in the page; you probably only want a specific element.
If you have IE11 installed, you can call querySelector, passing in a CSS selector:
With IE.document
Dim elem As Object
Set elem = .querySelector("#etpa-iframe > html > body > store-manager > div > side-bar > div > navigation > ul")
elem.click
End With
If you want to select the second li under the navigation, you can use the :nth-child selector:
Set elem = .querySelector("#etpa-iframe > html > body > store-manager > div > side-bar > div > navigation > ul > li:nth-child(2) > a")
来源:https://stackoverflow.com/questions/42262485/click-in-a-button