【问题标题】:Retrieving a document from a href从 href 检索文档
【发布时间】:2019-09-06 01:54:17
【问题描述】:

我正在为从网站下载信息制作代码。我的问题包括访问带有子菜单的下拉列表中的 Href。我尝试了很多代码,但其中任何一个都有效。

使用 getElementById 或 getElementsByClassName 的引用不起作用,因为此对象不支持函数 focus、click、selecteditems ="1"。下拉菜单的特殊性是首先通过单击进行管理,然后通过聚焦来管理它们。第一次单击三个点后,它会悬停一个名为“导出”的独特选项,接近它会悬停另外三个选项。我需要最后一个名为“下载相关扣除”。这是具体标签的代码。

<a title="Download associated deductions" class="ajax" href="exportPaymentLineItems.lvp?requestUID=&amp;reportType=xls&amp;reportName=Payments and associated deductions&amp;ajax=true&amp;isDrillable=" target="_blank">
    <span class="prgx-icon excel-icon"></span> Download associated deductions
</a>

这是我在 VBA 中的代码

Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
'...

IE.Document.getElementsByClassName("ajax").Click

之前在HTML代码中,第一次点击打开下拉菜单

<a class="btn btn-dots-vertical" id="dLabel" role="button" aria-expanded="true" href="#" data-toggle="dropdown" data-target="#">
</a>
<ul class="dropdown-menu multi-level dropdown-menu-right" role="menu" aria-labelledby="dropdownMenu">
    <li class="dropdown-submenu">
        <a tabindex="-1" href="#">Export</a>
        <ul class="dropdown-menu dropdown-menuright">

            <li><a title="Excel" href="supplierReport.lvp?requestUID=&amp;reportType=xls&amp;reportName=BasicClaimsPaymentReport" target="_blank"><span class="prgx-icon excel-icon"></span>Excel</a></li>
            <li><a title="CSV" href="supplierReport.lvp?requestUID=&amp;reportType=csv&amp;reportName=BasicClaimsPaymentReport" target="_blank"><span class="prgx-icon csv-icon"></span>CSV</a></li>
            <li><a title="PDF" href="supplierReport.lvp?requestUID=&amp;reportType=pdf&amp;reportName=BasicClaimsPaymentReport" target="_blank"><span class="prgx-icon pdf-icon"></span>PDF</a></li>

            'The one I need to click, download or copy to open in another explorer tab

            <li>
                <a title="Download associated deductions" class="ajax" href="exportPaymentLineItems.lvp?requestUID=&amp;reportType=xls&amp;reportName=Payments and associated deductions&amp;ajax=true&amp;isDrillable=" target="_blank">
                    <span class="prgx-icon excel-icon"></span> Download associated deductions
                </a>
            </li>

        </ul>
    </li>
</ul>

为了下载文档,我希望单击正确的类,复制 Href 并粘贴到新的资源管理器选项卡中,或者只是使用查询扇区启动下载。

【问题讨论】:

    标签: html vba web-scraping drop-down-menu


    【解决方案1】:

    试试属性选择器

    ie.document.querySelector("[title=Excel]").click
    
    ie.document.querySelector("[title='Download associated deductions']").click
    

    您的线路:

    IE.Document.getElementsByClassName("ajax").Click
    

    如果仅仅因为该方法返回一个集合将不起作用。您将需要一个索引,例如

    IE.Document.getElementsByClassName("ajax")(0).Click
    

    querySelector 返回单个节点。在 ""

    之间传递给它的 css 选择器的第一个匹配项

    【讨论】:

    • 谢谢。由于运行时错误“424”(需要对象),它仍然无法工作。你知道它指的是什么对象吗?
    • 第二个代码 ...("ajax")(0).Click 出现运行时错误“91”(未设置对象变量或块变量)。
    • 它指的是错误标记行的对象。通常是三件事中的一件。 1) 计时 - 尝试在该行之前添加 Application.Wait Now + TimeSerial(0,0,3) 2) 路径错误 - 检查元素不在父框架或 iframe 内 3) 元素不存在 - 检查 html
    • 我确认它是一个 iframe。我能做什么?
    • 它在 iframe 中?您可以在其中包含 iframe 的 html 和您的元素吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 2018-05-13
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多