尝试使用 querySelector 方法和[attribute^=value] CSS 选择器,它将选择每个 href 属性值以特殊值开头的元素。
示例代码如下(它将选择 href 属性值以 开头的 a 标签website/report/download.json):
Public Sub ClickTest()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate2 "<the website url>"
While .Busy Or .readyState <> 4: DoEvents: Wend
ie.Document.querySelector("a[href^='website/report/download.json']").Click
End With
End Sub
此外,您还可以使用getelementsbytagname方法查找标签,然后使用for语句循环遍历结果,并根据innerText属性查找特殊链接。最后,点击它。
编辑
您可以检查以下代码:
Public Sub ClickTest()
Dim ie As Object
Dim itemlist As Object 'Define a object to store the a tag list.
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate2 "<the website url>"
While .Busy Or .readyState <> 4: DoEvents: Wend
'ie.Document.querySelector("a[href^='website/report/download.json']").Click
Set itemlist = ie.document.getElementsByTagName("a")
'Debug.Print itemlist.Length ' check the count of a tag
If Len(itemlist) > 0 Then
'using For Each statement to loopthough the a tag list.
For Each Item In itemlist
'Debug.Print Item.innerText ' check the value
'If the value is "GeneralExtract.xlsx", click the link and exit the for statement.
If Item.innerText Like "GeneralExtract.xlsx" Then
Item.Click
Exit For
End If
Next Item
End If
End With
End Sub