如何访问 HTML collection in VBA 中的元素?
How do I access elements inside of an HTML collection in VBA?
我用下面的代码构建了一个collection,里面有几个项目。但是,我现在不知道如何访问这些项目或它们的属性。例如,这些项目之一是一个复选框,其中有 属性 Checked,可以是 true 或 false。我尝试过但失败的事情:
objCollection.item5
objCollection(5)
objCollection.Item(5)
这是我的代码:
Dim objIE As InternetExplorer 'special object variable representing the IE browser
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page
objIE.Navigate "https://secure01b.chase.com/web/auth/dashboard#/dashboard/overviewAccounts/overview/index"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop
Set objCollection = objIE.Document.getElementsByTagName("iframe")(0).contentDocument.getElementsByTagName("input")
整个过程的最终目标是能够通过宏登录到此页面以检索我的信用支出,以便我可以进行一些 excel 数据操作。
编辑:这是 运行 代码的结果,当我观看 collection 时:
objCollection Watch Results
索引从 0 开始,所以如果你想要第 5 个元素,你需要索引 4。也可以使用 .item(4) 或只是 (4)
objCollection.item(4)
objCollection(4)
集合中的项目数来自
HTMLItemCol.length
正如QHarr所说,Collection列表的索引从0开始,如果要查找第5个元素,我们可以使用objCollection(4)
或objCollection.item(4)
。
此外,我们还可以通过getElementbyId方法找到特殊元素,像这样:
Dim userText As Object, rememberCheckBox As Object
Set userText = objIE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("userId-input-field")
userText.Value = "AAA" 'set value.
Set rememberCheckBox = objIE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("input-useToken")
rememberCheckBox.Click 'click the useToken checkbox to check it.
在使用 F12 开发人员工具检查您的网站元素后,我发现您使用 checkbox__input checkbox__input--checked
CSS class 来检查复选框,而不是使用 checked
属性。你可以使用F12开发者工具来验证它。
所以,如果你要勾选复选框,找到复选框后,我们可以点击它来勾选。你可以参考上面的代码,或者使用下面的代码:
objCollection.Item(4).Click
objCollection(4).Click
我用下面的代码构建了一个collection,里面有几个项目。但是,我现在不知道如何访问这些项目或它们的属性。例如,这些项目之一是一个复选框,其中有 属性 Checked,可以是 true 或 false。我尝试过但失败的事情:
objCollection.item5
objCollection(5)
objCollection.Item(5)
这是我的代码:
Dim objIE As InternetExplorer 'special object variable representing the IE browser
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page
objIE.Navigate "https://secure01b.chase.com/web/auth/dashboard#/dashboard/overviewAccounts/overview/index"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop
Set objCollection = objIE.Document.getElementsByTagName("iframe")(0).contentDocument.getElementsByTagName("input")
整个过程的最终目标是能够通过宏登录到此页面以检索我的信用支出,以便我可以进行一些 excel 数据操作。
编辑:这是 运行 代码的结果,当我观看 collection 时: objCollection Watch Results
索引从 0 开始,所以如果你想要第 5 个元素,你需要索引 4。也可以使用 .item(4) 或只是 (4)
objCollection.item(4)
objCollection(4)
集合中的项目数来自
HTMLItemCol.length
正如QHarr所说,Collection列表的索引从0开始,如果要查找第5个元素,我们可以使用objCollection(4)
或objCollection.item(4)
。
此外,我们还可以通过getElementbyId方法找到特殊元素,像这样:
Dim userText As Object, rememberCheckBox As Object
Set userText = objIE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("userId-input-field")
userText.Value = "AAA" 'set value.
Set rememberCheckBox = objIE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("input-useToken")
rememberCheckBox.Click 'click the useToken checkbox to check it.
在使用 F12 开发人员工具检查您的网站元素后,我发现您使用 checkbox__input checkbox__input--checked
CSS class 来检查复选框,而不是使用 checked
属性。你可以使用F12开发者工具来验证它。
所以,如果你要勾选复选框,找到复选框后,我们可以点击它来勾选。你可以参考上面的代码,或者使用下面的代码:
objCollection.Item(4).Click
objCollection(4).Click