0

I am trying to get the class="woocommerce-Price-amount amount and the text within that says 20.00

I Have tried various means such as findelementbyclass which is not supported because it is a compound.

I am currently trying to get it to work with findelementbycss and I get a no such element error.

<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
6
  • You need to remove the space .FindElementByCss(".woocommerce-Price-amount.amount") Commented Oct 21, 2021 at 18:22
  • it will still say "object var or with block var not set" Commented Oct 21, 2021 at 20:28
  • You have forgotten to use SET variableName =driver.FindElementByCss(".woocommerce-Price-amount.amount") Commented Oct 21, 2021 at 22:05
  • it now errors on the next line-Sheet1.Cells.Value = variableName.Text it says "application defined or object defined error" Commented Oct 22, 2021 at 2:07
  • 1
    got it. For any future newbies: Sheet1.Cells(1, 1).Value = variableName.Text. thank you very much for your help. Commented Oct 22, 2021 at 14:11

1 Answer 1

1

Try this:

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

Prints:

20.00

In case the above fails because of different excel versions, try the following:

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

Before running the above script, be sure to add the following reference to the library:

Microsoft HTML Object Library
Sign up to request clarification or add additional context in comments.

12 Comments

It will give an compile error when running the above and say "user-defined type not defined", I am trying to get the price from the page and insert into excel. when using the html.queryselector it is still saying undefined.
It's because of the variation of our excel versions. I'm using excel 2016. However, try this Html.querySelector(".woocommerce-Price-amount > [class$='currencySymbol']").NextSibling.NextSibling.NodeValue. I'm not sure if it will work as I suggested it hypothetically.
Also, check out the edit @Red.
with Html.querySelector(".woocommerce-Price-amount > [class$='currencySymbol']").NextSibling.NextSibling.NodeValue. "it says object with block var not set"
with Html.querySelector(".woocommerce-Price-amount").innerText it says "invalid use of property"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.