将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价格jsMainPrice jsProductPrice hideFromPro”)。Item(0).innerText

在这种情况下,如何修改代码获取价格(33.99)?

解决方法

您可以选择querySelectorAll类的所有元素(请参见示例),例如</af:panelGroupLayout> <af:panelGroupLayout id="pgl10" styleClass="itemAction" inlineStyle="padding:10.0px;"> <af:goLink styleClass="directoryLink" id="viewLink" rendered="#{!empty attrs.item.viewUrl}" destination="#{attrs.item.viewUrl}" targetFrame="_blank" disabled="false"> <af:outputLabel id="view-icon" styleClass="viewButton directoryHoverButton"/> <af:outputText id="view-label" styleClass="directoryButtonLabel" value="#{res['button.view']}"/> </af:goLink> </af:panelGroupLayout> <af:panelGroupLayout id="pgl11" styleClass="itemAction" inlineStyle="padding:10.0px;"> <af:goLink id="downloadLink" destination="#{attrs.item.downloadURL}" styleClass="directoryLink" disabled="false"> <af:outputLabel id="download-icon" styleClass="downloadButton directoryHoverButton"/> <af:outputText id="download-label" styleClass="directoryButtonLabel" value="#{res['button.download']}"/> </af:goLink> </af:panelGroupLayout> <af:panelGroupLayout id="pgl12" styleClass="itemAction" inlineStyle="padding:10px; vertical-align:bottom;"> <af:selectBooleanCheckbox id="dc_sbc1" autoSubmit="true" partialTriggers="dc_sbc1" valueChangeListener="#{viewScope.printingBean.printerCheckboxListener}"> </af:selectBooleanCheckbox> <af:outputText id="print-label" styleClass="directoryButtonLabel" value="Add"/> </af:panelGroupLayout>

使用document.querySelectorAll(".fpStriked,.fpStrikedBefore,.jsStriked")-您不能

UPD:如何从querySelectorAll获取内容

querySelectorAll返回HTML集合,您可以使用.forEach获取每个元素的内容

getElementsByClassName
let allItems = document.querySelectorAll(".jsProductPrice,.fpStriked,.jsStriked")

allItems.forEach(e=>{
  console.log(e.textContent)
})

,

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