Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用vba在Internet Explorer上单击带有部分href的超链接_Javascript_Vba_Internet Explorer_Web Scraping - Fatal编程技术网

Javascript 使用vba在Internet Explorer上单击带有部分href的超链接

Javascript 使用vba在Internet Explorer上单击带有部分href的超链接,javascript,vba,internet-explorer,web-scraping,Javascript,Vba,Internet Explorer,Web Scraping,嗨,我试图创建一个脚本,点击一个链接,我可以提供一个部分链接。如果有人能告诉我怎么做,那就太好了 <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 = C

嗨,我试图创建一个脚本,点击一个链接,我可以提供一个部分链接。如果有人能告诉我怎么做,那就太好了

<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

Set i=CreateObject(“InternetExplorer.Application”)
Dim idoc作为MSHTML.HTMLDocument
设置idoc=i.document
Set eles6=idoc.getElementsByTagName(“a”)
对于ELS6中的每个ele6
如果ele6.href=“fileName=GeneralExtract”,则
ele6.点击
其他的
如果结束

尝试使用querySelector方法和
[attribute^=value]
,它将选择href属性值以特殊值开头的每个元素

示例代码如下(它将选择一个标签,其href属性值以
网站/report/download.json
开头):

公共子点击测试()
模糊的物体
设置ie=CreateObject(“InternetExplorer.Application”)
与ie
.Visible=True
.导航2“”
忙时或准备时状态4:DoEvents:Wend
ie.Document.querySelector(“a[href^='website/report/download.json'])。单击
以
端接头
此外,还可以使用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
公共子点击测试()
模糊的物体
Dim itemlist As Object'定义一个对象来存储标记列表。
设置ie=CreateObject(“InternetExplorer.Application”)
与ie
.Visible=True
.导航2“”
忙时或准备时状态4:DoEvents:Wend
'ie.Document.querySelector(“a[href^='website/report/download.json'])。单击
Set itemlist=ie.document.getElementsByTagName(“a”)
“Debug.Print itemlist.Length”检查标记的计数
如果Len(itemlist)>0,则
'使用For Each语句循环遍历标记列表。
对于itemlist中的每个项目
“Debug.Print Item.innerText”检查值
'如果值为“GeneralExtract.xlsx”,请单击链接并退出for语句。
如果Item.innerText类似于“GeneralExtract.xlsx”,则
项目。单击
退出
如果结束
下一项
如果结束
以
端接头

您好,谢谢您的回答,您能告诉我如何使用href的innerText吗。我试了好几次,但都写不正确it@HanitSingh Saluja,你可以查看上面的代码。嗨。非常感谢它的工作,并解决了我的问题:)
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