【发布时间】:2020-09-15 16:54:20
【问题描述】:
我正在尝试使用 VBA 进行 IE 自动化,但在单击 span 元素时遇到了问题。
我想点击下面的元素。
<span class="x-btn-icon-el x-btn-icon-el-default-toolbar-small de-icon-export_item " id="KEY_TOOLTIP_Export-btnIconEl" role="presentation" data-ref="btnIconEl" unselectable="on"> </span>
以下是 HTML 页面的代码。由于是内网页面,所以不能发相同的链接。
<a tabindex="-1" class="x-btn x-unselectable x-box-item x-toolbar-item x-btn-default-toolbar-small x-btn-over" id="KEY_TOOLTIP_Export" style="margin: 0px; left: 32px; top: 2px; right: auto;" hidefocus="on" unselectable="on" componentId="KEY_TOOLTIP_Export" data-qtip="Export">
<span class="x-btn-wrap x-btn-wrap-default-toolbar-small " id="KEY_TOOLTIP_Export-btnWrap" role="presentation" data-ref="btnWrap" unselectable="on">
<span class="x-btn-button x-btn-button-default-toolbar-small x-btn-no-text x-btn-icon x-btn-icon-left x-btn-button-center " id="KEY_TOOLTIP_Export-btnEl" role="presentation" data-ref="btnEl" unselectable="on">
<span class="x-btn-icon-el x-btn-icon-el-default-toolbar-small de-icon-export_item " id="KEY_TOOLTIP_Export-btnIconEl" role="presentation" data-ref="btnIconEl" unselectable="on"> </span>
<span class="x-btn-inner x-btn-inner-default-toolbar-small" id="KEY_TOOLTIP_Export-btnInnerEl" data-ref="btnInnerEl" unselectable="on"> </span>
</span>
</span>
</a>
以下是我尝试使用其他参考帖子的选项。 通过 ID 找到 Span 元素,然后找到它的父元素,这似乎是标签。
doc.getElementById("KEY_TOOLTIP_Export-btnIconEl").ParentNode.Click
也尝试了类名。
doc.getElementsByClassName("x-btn-icon-el x-btn-icon-el-default-toolbar-small de-icon-export_item").Item(0).ParentNode.Click
我可以使用调试选项在父框架中找到元素,所以应该排除 iFrame,不是吗?
我对 HTML、JavaScript 很陌生,所以不知道还有什么要检查的。
【问题讨论】:
-
首先,请检查您发布的 html 资源,在 元素下,它包含一些 标签和一个 标签,缺少开始标签,例如this。是否所有 元素都在 标记内?其次,从你的代码来看,似乎多个元素会有相同的 ID 和 Class 值,在这种情况下,最好使用 getElementsByClassName 方法查找元素,然后根据索引查找特殊元素。最后,关于 iframe 标签,您可以使用 F12 开发者工具检查该元素是否在 iframe 标签中。
-
感谢@ZhiLv-MSFT 的输入。我现在更新了 HTML 代码。根据最新的 html 代码,您可以看到我们正在寻找的类名和 id 只有 1 个匹配项,并且我都尝试过。我尝试使用开发人员工具查找数据是否正在加载到 iFrame 中,但事实并非如此。你能帮忙吗?
标签: html vba internet-explorer browser-automation