首页 > 解决方案 > 将 getElementsByClassname 与多个元素一起使用

问题描述

我正在尝试使用 VBA 宏从网站检索价格。通常,以下代码可以正常工作(示例):

price = html.getElementsByClassName("fpStriked fpStrikedBefore jsStriked").Item(0).innerText

但是,当 HTML 在包含价格的类中有多个属性时,前面的代码无法检索信息。这是包含多个属性的类的示例

"span class="fpPrice price jsMainPrice jsProductPrice hideFromPro" itemprop="price" content="33.99" 33<sup>€99</sup>"

price = html.getElementsByClassName("fpPrice price jsMainPrice jsProductPrice hideFromPro").Item(0).innerText

在这种情况下,如何调整代码以获取价格(33.99)?

标签: vbaimport-from-excelgetelementsbyclassname

解决方案


在上面的代码中,getElementsByClassName 将选择具有所有 3 个 ("fpStriked fpStrikedBefore jsStriked") 类的所有元素,但您的 span 没有这些类可供选择。


推荐阅读