Excel VBA单击没有名称或ID且使用JSON的网站按钮 [英] Excel VBA click website button with no name or ID that uses JSON
问题描述
我正在使用excel VBA打开此网站:法律和一般,然后,需要单击资金价格和收费"按钮.
I am using excel VBA to open this website: Legal and General, Then it needs to click the "Fund prices and charges" button.
用chrome检查网页,我可以看到此按钮包含以下代码:
Inspecting the web page with chrome, I can see this button has the following code:
< div class =选定的标签" tabindex ="0" role ="button" aria-pressed ="true"> ...</div>
HTML查看源代码"建议使用脚本类型="application/JSON"
The HTML 'view source' suggests a script type="application/JSON"
我对这一切感到非常困惑.有人知道我如何选择此按钮吗?到目前为止,我有这段代码:
I'm very confused by all of this. Does anyone know how I can select this button? I have this section of code so far:
Set HTMLDoc = .document
Set objElement = HTMLDoc.getElementsByClassName("Select - Tab - Fund prices and charges")(0)
objElement.Click
.Quit
任何帮助将不胜感激.
致谢
戴夫
推荐答案
如果仅对该选项卡感兴趣,我将首先使用css类选择器按其类名获取包含所有选项卡的父元素(div).然后,我将结合使用子组合器和nth-child()选择器,以按其类名抓取第二个子div:
If only interested in that one tab I would first grab the parent element (div), which houses all the tabs, by its classname using a css class selector. I would then use a child combinator in combination with nth-child() selector to grab the second child div by its classname:
.querySelector(".table-view__tabs > div:nth-child(2)").Click 'Select tab direct
如果可能对所有3个标签感兴趣:
If potentially interested in all 3 tabs:
我首先要使用css类选择器按其类名获取包含所有选项卡的父元素(div).然后,我将使用子组合器按其类名来获取子div(即选项卡).我将索引到返回的nodeList中以单击所需的选项卡:
I would first grab the parent element (div), which houses all the tabs, by its classname using a css class selector. I would then use a child combinator to grab the child divs (which are the tabs) by their classnames. I would index into the returned nodeList to click the required tab:
Set tabs = .querySelectorAll(".table-view__tabs > div") 'select all tabs then index in for tab of choice
tabs.Item(1).Click
VBA:
Option Explicit
Public Sub ClickButton()
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.Navigate2 "https://fundcentres.lgim.com/uk/workplace-employee/fund-centre/#Product=WorkSave-Pension-Plan-(generation-3)"
While .Busy Or .ReadyState <> 4: DoEvents: Wend
With .Document
.querySelector(".table-view__tabs > div:nth-child(2)").Click 'Select tab direct
Stop 'Delete me later
Dim tabs As Object
Set tabs = .querySelectorAll(".table-view__tabs > div") 'select all tabs then index in for tab of choice
tabs.Item(1).Click '0 based so first tab is 0, second is 1 etc
Stop 'Delete me later
End With
Stop '<==Delete me later
.Quit
End With
End Sub
阅读:
- CSS selectors
- Child combinator
- Class selector
- :nth-child
- document.querySelectorAll
- document.querySelector
参考(VBE>工具>参考):
- Microsoft Internet控件
这篇关于Excel VBA单击没有名称或ID且使用JSON的网站按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!