尝试通过 class 名称 VBA Selenium 获取元素

Trying to get element by class name VBA Selenium

我正在尝试获取 class="woocommerce-Price-amount 金额,其中的文本显示 20.00

我尝试了各种方法,例如findelementbyclass,因为它是复合的,所以不被支持。

我目前正在尝试让它与 findelementbycss 一起工作,但我收到了一个没有这样的元素的错误。

<div class="summary entry-summary">
        <div class="summary-container"><h2 itemprop="name" class="product_title entry-title">2&#8243;x 2&#8243; Alumi-guard fence END post for 4&#8242; tall fence</h2>
<p class="price"><span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">&#36;</span>20.00</bdi></span></p>
<div class="avada-availability">
    </div>
<div class="product-border fusion-separator sep-none"></div>

Private driver As Selenium.ChromeDriver
Sub test2()

Dim name As String

name = "test"
Set driver = New Selenium.ChromeDriver

Dim By As New By, variableName As WebElement

driver.Start "chrome"

driver.Get "https://www.kandmfence.net/product/2x-2-alumi-guard-fence-end-post-for-4-tall-fence/"

variableName = driver.FindElementByCss(".woocommerce-Price-amount .amount")

Sheet1.Cells.Value = variableName.Text

End Sub

试试这个:

Sub GetPrice()
    Const URL$ = "https://www.kandmfence.net/product/2x-2-alumi-guard-fence-end-post-for-4-tall-fence/"
    Dim Html As HTMLDocument
    
    Set Html = New HTMLDocument

    With CreateObject("MSXML2.XMLHTTP")
        .Open "Get", URL, False
        .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36"
        .send
        Html.body.innerHTML = .responseText
    End With

    MsgBox Html.querySelector(".woocommerce-Price-amount > [class$='currencySymbol']").NextSibling.NodeValue

End Sub

打印:

20.00

如果由于 excel 版本不同导致上述操作失败,请尝试以下操作:

Html.querySelector(".woocommerce-Price-amount").innerText

在运行上面的脚本之前,一定要添加下面的库引用:

Microsoft HTML Object Library