我正在使用excel VBA打开这个网站:法律和一般,然后它需要点击“基金价格和收费”按钮。
使用chrome检查网页时,我可以看到这个按钮有以下代码:
<div class=" selected tab" tabindex ="0" role="button" aria-pressed="true">...</div>
HTML‘视图源’建议一个脚本type="application/JSON“
我被这一切弄糊涂了。有人知道我怎么选择这个按钮吗?到目前为止,我有这部分代码:
Set HTMLDoc = .document
Set objElement = HTMLDoc.getElementsByClassName("Select - Tab - Fund prices and charges")(0)
objElement.Click
.Quit
任何帮助都将不胜感激。
问候
戴夫
发布于 2019-12-18 20:08:27
如果只对这个选项卡感兴趣,我将首先使用css类选择器通过其类名获取父元素(div),该元素包含所有选项卡。然后,我将使用一个子组合器与nth- child ()选择器结合使用它的类名来获取第二个子div:
.querySelector(".table-view__tabs > div:nth-child(2)").Click 'Select tab direct
如果可能对所有3个选项卡感兴趣:
首先,我使用css类选择器获取父元素(div),它包含所有选项卡,并使用它的类名。然后,我将使用一个子组合器来根据它们的类名获取子div(这是选项卡)。我将索引返回的nodeList以单击所需的选项卡:
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
阅读:
引用(VBE>Tools>References):
https://stackoverflow.com/questions/59395660
复制相似问题