[英]How to interact with IE iframe without iframe id
我需要單擊 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>
使用它的屬性之一。 例如,
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 選擇器進行了優化,因此這種方法速度很快。 使用querySelector也會在第一次匹配時停止,這再次提高了效率。
您也可以嘗試使用 getElementsbyTagName 方法先獲取 Iframe,然后根據 Iframe.contentDocument 屬性通過 class 名稱獲取元素。
示例代碼如下:
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 開始。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.