0

I am trying to read a value of price from a website using Excel VBA but I get an Error

Object does not support this property or method

The part of HTML

<div class="box-three box-right col-xs-20 col-lg-5 col-md-6 no-gutter">
<div class="inner-box-one">
<div class="price-container" itemprop="offerDetails" itemscope itemtype="http://data-vocabulary.org/Offer">
<span class="price-label">Τιμή:</span>
<span class="final-price"><span itemprop="price">594,00 €</span> </span>
<meta itemprop="currency" content="EUR" />
<meta itemprop="availability" content="in_stock" />
</div>

I am trying with this:

Dim getPrice As Object
Set getPrice = ie.Doc.getElementByClassName("final-price")
Dim myValue As String: myValue = getPrice.innerText
wks.Cells(i, "C").Value = myValue

2 Answers 2

2

How many of these are there on the page and is it the first? Your error is because you are trying to use a method for a single element on a collection as you haven't used an index on the collection to retrieve a single item.

You can return a nodeList of all matching classes with

Dim nodeList As Object, i As Long
Set nodeList = ie.document.querySelectorAll(".final-price")

Then access each by index:

For i = 0 To nodeList.Length-1
    Debug.Print nodeList.item(i).innerText
Next 

The "." is a class css selector. It targets the elements by their class name. The space between two class selectors represents a descendant combinator, stating that the second class must be a child of the first class.

.price-container .price-label

You may wish to grab labels and prices together:

Dim labels As Object, prices As Object, i As Long
Set labels = ie.document.querySelectorAll(".price-container .price-label")
Set prices = ie.document.querySelectorAll(".price-container .final-price")

For i = 0 To labels.Length - 1
    Debug.Print labels.item(i).innerText & " " & prices.item(i).innerText
Next

Which, in fact, you could further combine as:

Dim labelsPrices, i As Long
Set labelsPrices = ie.document.querySelectorAll(".price-container .price-label, .price-container .final-price")

For i = 0 To labels.Length - 1 Step 2
    Debug.Print labels.item(i).innerText & " " & prices.item(i + 1).innerText
Next

CSS selectors are applied via querySelector and querySelectorAll methods - in this case, of .document. The first returns a single element i.e. the first match, the second returns all matches.


Single item:

You can use the last bit of code directly above to get label and price as there would be two elements if a single price and single label.

Or

Dim label As Object, price As Object, i As Long
Set label= ie.document.querySelector(".price-container .price-label")
Set price = ie.document.querySelectorAll(".price-container .final-price")

Debug.Print label.innerText, price.innerText
Sign up to request clarification or add additional context in comments.

3 Comments

It is a single product page with one price, So I need nevertheless the nodelist?
how to display it to a cell? Debug.Print it will display it on selected cell?
Cells(1,1) = price.innerText. Etc
0
Dim getPrice As Object
Set getPrice = ie.Doc.getElementByClassName("final-price")(0) 'While Using Class Name you must indicate an index of that class
Dim myValue As String: myValue = getPrice.innerText
wks.Cells(i, "C").Value = myValue

2 Comments

And What is ie.doc? it should be ie.Document.getelementsbyClassName
Then use the edit link and include that information in your Answer, not as a comment below it. And don't use a question mark in an Answer - that would be a comment below the question. State it more like: ie.doc doesn't compile - it should be... and use the correct syntax in the code you provide. Especially since this is more likely the cause of the error message.

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.