excel - 即 getelementsbyID 具有相同的 ID

标签 excel vba internet-explorer

我有一个与 internet explorder (ie) 一起使用的脚本,我需要循环选择字段,这不是问题,但 4 个元素具有相同的 ID(在同一页面上)。

如何让它循环遍历 4 个字段? 我可以让它们更具体吗?

我使用的代码如下:

ie.document.getElementByID("DownloadImage").Click

ie代码如下: 字段 1

<a id="DownloadButton" href="javascript:__doPostBack('ctl00$ctl00$MainContent$MainContent$ctl00$declaratiebestandView$RetourInformatieGrid$ctl03$DownloadButton','')">CZ_Specificatie_150005697.pdf</a>

字段 2

<a id="DownloadButton" href="javascript:__doPostBack('ctl00$ctl00$MainContent$MainContent$ctl00$declaratiebestandView$RetourInformatieGrid$ctl03$DownloadButton','')">CZ_Specificatie_150005697.pdf</a><input name="ctl00$ctl00$MainContent$MainContent$ctl00$declaratiebestandView$RetourInformatieGrid$ctl03$DownloadImage" class="inlineButton" id="DownloadImage" type="image" src="../images/download.png" text="CZ_Specificatie_150005697.pdf">

然后它打开下载屏幕,然后我的代码继续(并且有效:))

最佳答案

您可以通过使用 querySelectorAll 来收集具有 id 属性(其值与您之后的值匹配的值)的所有元素来循环它们。您可以通过索引来区分它们。即使 ID 重复,此方法也可以让您收集它们。但是,您共享的 HTML 下载相同的文档,因此循环似乎没有必要。

Dim nodeList As Object, i As Long
Set nodeList = ie.document.querySelectorAll("[id=DownloadButton]")
For i = 0 to nodeList.Length-1
    nodeList.item(i).Click
Next

循环所有匹配的元素并点击

按索引将是具体的,但如果您熟悉 CSS 选择器,则指定元素的可能性有很多。

关于excel - 即 getelementsbyID 具有相同的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52736627/

相关文章:

python - 使用 pywin32/excel 在 Windows 上将 Python 移植到 Vagrant 机器上的 Linux

vba - 将满足范围内的值的行复制到sheet2

excel - 处理 Visual Basic for Applications 中缺少 Internet 连接的问题

c# - BHO 插件(Microsoft JScript 运行时错误)

c# - 无法打开从 c# 生成的 Excel

vba - 如何将 excel VBA UI 放在前面

excel - Access VBA(打开excel文件并关闭): turn off "file now available"

vba - 在 Excel 2013 中删除相对选择范围内的图片

javascript - jQuery/JS - IE 错误 - 对象不支持属性或方法 'hasClass'

html - 为什么不溢出 :hidden working in IE6?