VBA: 在 IE 中引用图像
VBA: Referencing an image in IE
我正在尝试自动下载报告。我需要按下一个图像,但我无法使用 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
有什么想法吗?
谢谢
尝试此示例代码与您的 HTML 一起使用并能够点击该图像。
VBA代码:
Sub demo()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate ("D:\Backup20190913\tests7.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 中的输出:
另外,您可以根据自己的需要修改代码。
如果确实是要点击的 img,您可以使用 attribute = value css 选择器来定位。无需循环,浏览器针对 css 选择进行了优化。
appIE.document.querySelector("[data-fb-link='Excel Download']").click
另一个
appIE.document.querySelector("[data-fb-button='Leads Grid Refresh']").click
我正在尝试自动下载报告。我需要按下一个图像,但我无法使用 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
有什么想法吗?
谢谢
尝试此示例代码与您的 HTML 一起使用并能够点击该图像。
VBA代码:
Sub demo()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate ("D:\Backup20190913\tests7.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 中的输出:
另外,您可以根据自己的需要修改代码。
如果确实是要点击的 img,您可以使用 attribute = value css 选择器来定位。无需循环,浏览器针对 css 选择进行了优化。
appIE.document.querySelector("[data-fb-link='Excel Download']").click
另一个
appIE.document.querySelector("[data-fb-button='Leads Grid Refresh']").click