【问题标题】:How to interact with IE iframe without iframe id如何在没有 iframe id 的情况下与 IE iframe 交互
【发布时间】:2020-02-01 00:55:32
【问题描述】:

我需要单击 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>

【问题讨论】:

  • 请不要张贴代码作为截图。获取您的代码并将其直接发布在问题中,以便有人可以在他们的机器上复制您的问题。
  • 已添加代码。

标签: javascript html excel vba internet-explorer


【解决方案1】:

使用它的属性之一。例如,

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 也会在第一次匹配时停止,这再次提高了效率。

【讨论】:

  • 感谢 Qharr,我已经尝试了下面的代码,但是它不起作用.. If waitElement("iframe", "title", "data visualization", 30) = True Then For Each ele In ie.document.getElementsByTagName("iframe") If ele.getAttribute("title") = "data visualization" Then ele.contentDocument.querySelector("[class='tabToolbarButton tab-widget download']").Click Exit For End If Next ele Else MsgBox "fail" End If
  • 我不能使用 querySelector(".tabToolbarButtonText"),因为有很多这样的类型只在内部文本上有所不同。
  • 您的意思是许多具有相同类的项目仅在 innerText 上有所不同?没有其他属性可以区分或关联到唯一标识的另一个元素?
  • 是的,就像这个是 iframe 中的另一个按钮。&lt;div tabindex="-1" class="tabToolbarButton tab-widget share" role="button" style="width: 75px; -ms-touch-action: none;" aria-label="Share"&gt;&lt;span class="tabToolbarButtonImg tab-icon-share"&gt;&lt;/span&gt;&lt;span class="tabToolbarButtonText"&gt;Share&lt;/span&gt;&lt;/div&gt;
  • 您是否尝试过 ie.document.querySelector("[title='数据可视化']").contentDocument.querySelector(".download").click 本身?没有循环
【解决方案2】:

您也可以尝试使用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 开始。

【讨论】:

  • 谢谢!有没有一种简单的方法来获取元素的索引?浏览 HTML 文档很困难。
  • 如果 Iframe 数量是固定的,您可以使用 F12 开发者工具 DOM Explorer 查找 Iframe 并获取索引。截图如this。否则,您可以尝试为 iframe 添加不同的类名或 id 属性。
猜你喜欢
  • 2020-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-26
  • 2017-07-02
  • 1970-01-01
  • 2016-05-23
相关资源
最近更新 更多