Javascript VBA:如何在图像上没有ID或名称的情况下触发onclick事件?

Javascript VBA:如何在图像上没有ID或名称的情况下触发onclick事件?,javascript,html,excel,vba,web-scraping,Javascript,Html,Excel,Vba,Web Scraping,我正在尝试使用VBA为IE中的imagine启动onclick事件。图像只有Onclick、alt和src。我不知道如何循环到图像,然后启动onclick 我已经尝试过几种不同的方法,但没有一种方法适用于我的具体案例。通常,它们包含内部文本,而我没有 Excel VBA(这是错误的,但我希望这样) JavaScript function fnForecast_Export() { try { var sSearch = txtSearch.value.trim().sq

我正在尝试使用VBA为IE中的imagine启动onclick事件。图像只有Onclick、alt和src。我不知道如何循环到图像,然后启动onclick

我已经尝试过几种不同的方法,但没有一种方法适用于我的具体案例。通常,它们包含内部文本,而我没有

Excel VBA(这是错误的,但我希望这样)


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



我希望网页javascript中的“fnfecast_Export()”函数能够运行。

如果您不能使用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

如果您不能使用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


ieDoc.parentWindow.execScript“fnForecast_Export();”ieDoc.querySelector(“[alt='Export to Excel']”)。FireEvent“onclick”或只是ieDoc.querySelector(“[alt='Export to Excel']”)。单击“重试”,如果工作需要,请告知我可以作为副本关闭。我已经尝试了您给出的所有建议。第一次执行javascript仍然会出现80020101错误。我已经在代码中执行了一些javascript,但由于某些原因,这不起作用。其他两个建议都给出了“对象不支持此属性或方法错误”。“.querySelector”是否适合与HTMLDocument一起使用?querySelector是HTMLDocument的一种方法。值得使用IE>仿真选项卡>检查文档模式。它应该是最新的,例如Edge | 11等。。。最后一种选择是ieDoc.parentWindow.execScript“document.querySelector”(“[alt='Export to Excel']”)。单击();”,但再次单击-检查文档模式。文档模式被设置为网页的兼容模式。页面将仅在文档模式5下运行。它不适用于11.ieDoc.parentWindow.execScript“fnForecast_Export();”ieDoc.querySelector(“[alt='Export to Excel']”)。FireEvent“onclick”或只是ieDoc.querySelector(“[alt='Export to Excel']”)。单击“试用”,如果需要,请告知我可以作为副本关闭。我已经尝试了您给出的所有建议。第一次执行javascript仍然会出现80020101错误。我已经在代码中执行了一些javascript,但由于某些原因,这不起作用。其他两个建议都给出了“对象不支持此属性或方法错误”。“.querySelector”是否适合与HTMLDocument一起使用?querySelector是HTMLDocument的一种方法。值得使用IE>仿真选项卡>检查文档模式。它应该是最新的,例如Edge | 11等。。。最后一种选择是ieDoc.parentWindow.execScript“document.querySelector”(“[alt='Export to Excel']”)。单击();”,但再次单击-检查文档模式。文档模式被设置为网页的兼容模式。页面将仅在文档模式5下运行。它不能与11一起工作。没有错误,但是循环被完全跳过。代码运行到“For Each”行,然后经过循环。我很好奇它是否检测不到任何标记名为“img”的对象。imgs.length的值是多少?如果用F8一行一行地执行代码,慢慢地等待页面加载,这会改变吗?好的,我添加了一个dowhile循环,在执行循环之前等待页面加载。这似乎有所帮助,但现在我遇到了一个运行时错误70“权限被拒绝”。这在使用img.length或运行循环时发生。我猜这个页面被保护了?请在哪一行?在imgs.length行上。没有错误,但是循环被完全跳过了。代码运行到“For Each”行,然后经过循环。我很好奇它是否检测不到任何标记名为“img”的对象。imgs.length的值是多少?如果用F8一行一行地执行代码,慢慢地等待页面加载,这会改变吗?好的,我添加了一个dowhile循环,在执行循环之前等待页面加载。这似乎有所帮助,但现在我遇到了一个运行时错误70“权限被拒绝”。这在使用img.length或运行循环时发生。我猜这页被保护了?请在哪一行?在imgs.length行。
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
'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