0

您好我正在尝试创建一个脚本来单击我可以提供部分链接的链接。如果有人可以建议我如何做到这一点,那就太好了

<a href="website/report/download.json?refId=3e49762e-8edc-47c2-a282-11ee3c64e85a&amp;reportType=xlsx&amp;fileName=GeneralExtract.xlsx&amp;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
4

1 回答 1

0

尝试使用 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
于 2020-01-16T16:04:17.487 回答