使用 vba 在 Internet Explorer 上单击带有部分 href 的超链接
Clicking on hyperlink with partial href on Internet Explorer using vba
您好,我正在尝试创建一个脚本来点击 link,我可以提供部分 link。如果有人可以告诉我如何做到这一点,那就太好了
<a href="website/report/download.json?refId=3e49762e-8edc-47c2-a282-11ee3c64e85a&reportType=xlsx&fileName=GeneralExtract.xlsx&obo>GeneralExtract.xlsx</a>
Set i = CreateObject("InternetExplorer.Application")
Dim idoc As MSHTML.HTMLDocument
Set idoc = i.document
Set eles6 = idoc.getElementsByTagName("a")
For Each ele6 In eles6
If ele6.href = "fileName=GeneralExtract" Then
ele6.Click
Else
End If
尝试使用 querySelector 方法和 [attribute^=value]
CSS selector,它将 selects 每个 href 属性值以特殊值开头的元素。
示例代码如下(将select href属性值以website/report/download.json
开头的a标签):
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方法找到tag,然后用for语句循环结果,根据innerText属性找到特殊的link。最后,点击它。
编辑
您可以查看以下代码:
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
您好,我正在尝试创建一个脚本来点击 link,我可以提供部分 link。如果有人可以告诉我如何做到这一点,那就太好了
<a href="website/report/download.json?refId=3e49762e-8edc-47c2-a282-11ee3c64e85a&reportType=xlsx&fileName=GeneralExtract.xlsx&obo>GeneralExtract.xlsx</a>
Set i = CreateObject("InternetExplorer.Application")
Dim idoc As MSHTML.HTMLDocument
Set idoc = i.document
Set eles6 = idoc.getElementsByTagName("a")
For Each ele6 In eles6
If ele6.href = "fileName=GeneralExtract" Then
ele6.Click
Else
End If
尝试使用 querySelector 方法和 [attribute^=value]
CSS selector,它将 selects 每个 href 属性值以特殊值开头的元素。
示例代码如下(将select href属性值以website/report/download.json
开头的a标签):
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方法找到tag,然后用for语句循环结果,根据innerText属性找到特殊的link。最后,点击它。
编辑
您可以查看以下代码:
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