【问题标题】:VBA: How do I fire onclick event with no IDs or Names on image?VBA:如何在图像上没有 ID 或名称的情况下触发 onclick 事件?
【发布时间】:2019-12-05 14:11:19
【问题描述】:

我正在尝试使用 VBA 在 IE 中为想象触发 onclick 事件。 Image 只有 Onclick、alt 和 src。我不知道如何循环到图像然后触发 onclick。

我已经尝试过几种不同的循环方式,但没有一种解决方案适用于我的具体情况。通常,它们涉及到内部文本,而我没有。

Excel VBA(这是错误的,但我想要这样的东西)

  Dim elements As Object, element As Object
    Set elements = ieDoc.getElementsByTagName("img")

    For Each element In elements
    If element.innerText = "Export to Excel" Then
        element.Click
    End If
    Next

JavaScript

function fnForecast_Export() {
    try {
        var sSearch = txtSearch.value.trim().sqlEscape().xmlToSpecial();
        if (sSearch === "") {fnGlobal_Alert(g_sBLANKSEARCH); return;}

        var sSQL = "qryGetCustomerForecastList 'SearchExport','" + fnSQLWild(sSearch, false) + "','" +
            txtStartDate.value + "','" + txtEndDate.value + "'," + drpCustomer.value + "," +
            drpPlant.value + "," + g_iPageID + ",'" + drpSortOrder.value + "'," + drpSortDirection.value;

        var sURL = g_sAppPath + "forecasting/ForecastExportWindow.asp" +
            "?Rnd=" + Math.random() +
            "&PageName=Customer" +
            "&SQL=" + sSQL +
            "&StartDate=" + g_sStartDate +
            "&EndDate=" + g_sEndDate;

        top.fraHeader.fnWait("Exporting...", "", 15000);

        var oFrame = document.getElementById("fraExportFrame");

        if (oFrame !== null) {
            oFrame.src = sURL;
        } else {
            var oNewFrame = document.createElement("IFRAME");
            oNewFrame.id = "fraExportFrame";
            oNewFrame.src = sURL;
            oNewFrame.style.height = "0px";
            oNewFrame.style.width = "0px";
            document.appendChild(oNewFrame);
        }
    } catch(e) {
        fnErrorHandler(e, "");
    }}

HTML

<TD id=tdBlock class=tdCriteriaBlock noWrap>&nbsp;
<IMG onclick=fnForecast_Search(); alt=Search src="/PE372/images/search.gif">&nbsp;&nbsp;
<IMG onclick=fnForecast_Export(); alt="Export to Excel" src="/PE372/images/excel.gif">&nbsp;&nbsp;
<IMG onclick=fnForecast_SearchClear(); alt="Clear Search Criteria" src="/PE372/images/eraser.gif">&nbsp;&nbsp;</TD>

我想运行网页javascript中的“fnForecast_Export()”函数。

【问题讨论】:

  • ieDoc.parentWindow.execScript "fnForecast_Export();"
  • ieDoc.querySelector("[alt='Export to Excel']").FireEvent "onclick" 或者只是 ieDoc.querySelector("[alt='Export to Excel']").click 试试这些,如果工作让我知道,因为我可以作为副本关闭。
  • 我已经尝试了您提供的所有建议。第一次执行 javascript 仍然给出 80020101 错误。我已经在代码中执行了一些 javascript,但这只是由于某种原因不起作用。其他两个建议都给了我“对象不支持此属性或方法错误”。 “.querySelector”是否适合与 HTMLDocument 一起使用?
  • querySelector 是 HTMDocument 的一个方法。值得去 IE > Emulation 选项卡> 检查文档模式。它应该是最新的,例如边缘 | 11 等等...最后一个替代方案是 ieDoc.parentWindow.execScript "document.querySelector(""[alt='Export to Excel']"").click();"但再次 - 检查文档模式。
  • 文档模式设置为网页的兼容模式。该页面仅在文档模式 5 下运行。它不适用于 11。

标签: javascript html excel vba web-scraping


【解决方案1】:

如果您不能使用 querySelector,那么您将需要尝试使用循环并考虑在框架标签中。

Dim imgs As Object, img As MSHTML.HTMLImg
Set imgs = ieDoc.getElementsByTagName("frame")(0).contentDocument.getElementsByTagName("img") 'you may need to change index from 0 to appropriate one.
For Each img In imgs
    If img.hasAttribute("alt") Then
        If img.getAttribute("alt") = "Export to Excel" Then
            ''choose your option
            img.Click
            'img.FireEvent "onclick"
            Exit For
        End If
    End If
Next

【讨论】:

  • 没有错误,但是完全跳过了Loop。代码运行到“For Each”行,然后移过循环。我很好奇它是否没有检测到任何带有标签名称“img”的对象。
  • imgs.length 的值是多少?如果您使用 F8 缓慢等待页面加载逐行执行代码,这种情况会改变吗?
  • 好的,所以我添加了一个 dowhile 循环来等待页面加载,然后再执行循环。这似乎有所帮助,但现在我收到运行时错误 70“权限被拒绝”。使用 img.length 或运行循环时会发生这种情况。我猜该页面是受保护的?
  • 请问在哪一行?
  • 在 imgs.length 行上。
【解决方案2】:
'Set frame to destination frame in webpage
Set frame = ieDoc.getElementsByName("fraMain")(0) 'change fraMain to your specific frame

'set ieDoc to content within specified frame
Set ieDoc = frame.contentDocument

'Set imgs to objects with tagname "img"
Set imgs = ieDoc.getElementsByTagName("img")

For Each img In imgs
        If img.hasAttribute("alt") Then
            If img.getAttribute("alt") = "Export to Excel" Then 'change "Export to Excel" to your specific alt.
                ''choose your option
                'img.Click
                img.FireEvent "onclick"
                Exit For
            End If
        End If
Next

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    相关资源
    最近更新 更多