我需要在iframe中单击div才能触发下载。我很难定位iframe元素,因为它没有ID。这是iframe元素:
<iframe title="data visualization" src="https://sample.com" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" allowfullscreen="true" style="width: 100%; height: 100%; display: block; visibility: visible;" allowtransparency="true"></iframe>
这是iframe中的div元素:
<div tabindex="-1" class="tabToolbarButton tab-widget download" role="button" style="width: 95px; -ms-touch-action: none;" aria-label="Download">
<span class="tabToolbarButtonImg tab-icon-download"></span>
<span class="tabToolbarButtonText">Download</span>
</div>
答案 0 :(得分:0)
使用其属性之一。例如,
ie.document.querySelector("[title='data visualization']").contentDocument.querySelector(".tabToolbarButtonText").click 'change this end bit for the appropriate element to click
也许
ie.document.querySelector("[title='data visualization']").contentDocument.querySelector(".download").click 'change this end bit for the appropriate element to click
或者直接是src
属性值:
ie.Navigate "https://sample.com"
现代浏览器针对css selectors进行了优化,因此该方法非常快捷。使用querySelector也会在第一次比赛时停止,这也是效率的提高。
答案 1 :(得分:0)
您还可以尝试使用getElementsbyTagName方法首先获取Iframe,然后根据Iframe.contentDocument属性通过类Name获取元素。
示例代码如下:
Sub Test()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate "<the website url>"
While ie.readyState <> 4
DoEvents
Wend
ie.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementsbyClassName("tabToolbarButton tab-widget download")(0).Click
End With
Set ie = Nothing
End Sub
[注意]使用getElementsbyTagName和getElementsbyClassName时,请注意元素的索引。从0开始。