VBA: ссылка на изображение в IE - PullRequest
1 голос
/ 23 сентября 2019

Я пытаюсь автоматизировать загрузку отчета.Мне нужно нажать на изображение, но я не могу найти его, используя 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 голосов
/ 23 сентября 2019

Если кликнуть на img, вы можете использовать селектор attribute = value css для цели.Цикл не требуется, и браузеры оптимизированы для выбора CSS.

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

Для других

appIE.document.querySelector("[data-fb-button='Leads Grid Refresh']").click
0 голосов
/ 23 сентября 2019

Попробуйте этот пример кода работать с вашим 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

Кроме того, вы можете изменить код в соответствии со своими требованиями.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...