VBA:引用IE中的图像

时间:2019-09-23 11:14:24

标签: excel vba internet-explorer web-scraping automation

我正在尝试自动下载报告。我需要按一张图像,但是我无法通过使用getElementsByClassName,getElementsByTagName或getElementById找到它。图片的代码如下:

<div class="right">
<a data-bind="click: function(){window.location = leads.list.url_get('.xls');}" style="cursor: pointer;"> <img data-bind="img: 'excel_36x36.gif'" data-fb-link="Excel Download" style="margin-top:-5px;margin-right:5px;" src="/assets/excel_36x36-55ac129b7404a6db9f6e3f43d2ec79982d1d89cdbc6d8d340befd029b4e79140.gif" width="24px"> </a>
Application Status: 
<select data-bind="value: leads.overall_status, options: [null, 'not_started', 'quoted_with_quotes', 'quoted_without_quotes', 'started', 'open_accounts'], optionsText: BrokerLeadsGridModel.overall_status_renderer" style="width: 160px;vertical-align: baseline;display: inline;" class="form-control"><option value="">Any</option><option value="not_started">Not Started</option><option value="quoted_with_quotes">Quoted</option><option value="quoted_without_quotes">No Quotes</option><option value="started">Started</option><option value="open_accounts">Open</option></select> 
Date Field: 
<select data-bind="value: leads.date_range_choice, options: ['created_at', 'terminal_at'], optionsText: BrokerLeadsGridModel.date_range_choices_text" style="width:125px; vertical-align: baseline; display: inline" class="form-control"><option value="created_at">Created At</option><option value="terminal_at">Terminal At</option></select> 
Date Range: 
<input data-bind="daterange: leads.daterange" spellcheck="false" class="form-control" style="margin-right:5px; width: 230px;vertical-align: baseline;display: inline;">
<img data-bind="img: 'refresh_22x22.png', click: leads.list.load" style="margin-top: -6px; cursor: pointer;" data-fb-button="Leads Grid Refresh" src="/assets/refresh_22x22-241f7c63a911337da4bb398fab9a562d155c836e077308c99908a9ed4e687e77.png">
</div>

除了帮助我尝试找到正确的元素之外,我的代码目前无能为力:

        Set imgCollection = appIE.document.getElementsByTagName("img").Value

        For Each c In imgCollection
            MsgBox c
        Next

有什么想法吗?

谢谢

2 个答案:

答案 0 :(得分:0)

请尝试使用此示例代码来处理您的HTML,并能够单击该图像。

VBA代码:

    Sub demo()

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

    IE.navigate ("D:\Backup20190913\tests\257.html")


    While IE.readyState <> 4
        DoEvents
    Wend

    Dim oHDoc As HTMLDocument
    Set oHDoc = IE.document


    Dim iCnt As Integer
    Dim path As String
    path = "/assets/refresh_22x22-241f7c63a911337da4bb398fab9a562d155c836e077308c99908a9ed4e687e77.png"

        For iCnt = 0 To oHDoc.getElementsByTagName("img").Length - 1
            If path = oHDoc.getElementsByTagName("img").Item(iCnt).src Then
            oHDoc.getElementsByTagName("img").Item(iCnt).Click
            End If
        Next iCnt



   ' IE.Quit
   ' Set IE = Nothing
   ' Set oHEle = Nothing
   ' Set oHDoc = Nothing
End Sub

HTML代码:

<html>
<head>
<script>
function abc()
{
   alert("image clicked...")
}
</script>
</head>
<body>
<img onclick="abc()" data-bind="img: 'refresh_22x22.png', click: leads.list.load" style="margin-top: -6px; cursor: pointer;" data-fb-button="Leads Grid Refresh" src="/assets/refresh_22x22-241f7c63a911337da4bb398fab9a562d155c836e077308c99908a9ed4e687e77.png">

</body>
</html>

IE 11中的输出:

enter image description here

此外,您可以根据自己的要求修改代码。

答案 1 :(得分:0)

如果确实是要单击的img,则可以使用attribute = value css选择器进行定位。无需循环,浏览器针对CSS选择进行了优化。

 appIE.document.querySelector("[data-fb-link='Excel Download']").click

其他

appIE.document.querySelector("[data-fb-button='Leads Grid Refresh']").click