1

I'm new using selenium. I have a site that is not more compatible with the IE, so i decided to try this new technique, but can't see what is wrong on my code. Any help will be apreciated.

 Sub ExtractPrice()
    
        Dim bot As WebDriver, myproducts As WebElements, myproduct As WebElement
        Set bot = New WebDriver
        bot.Start "chrome"
        bot.Get "https://www.veadigital.com.ar/prod/72060/lechuga-capuchina-por-kg"
    '    Application.Wait Now + TimeValue("00:00:20")
        Set myproducts = bot.FindElementsByClass("datos-producto-container")
    '
    For Each myproduct In myproducts
    If myproduct.FindElementByClass("product-price").Text <> "" Then
    'Debug.Print myproducts.FindElementByClass("product-price").Text
        Worksheets("VEA").Range("b2").Value = myproducts.FindElementsByClass("product-price").Text
        End If
    Next
    
    MsgBox ("complete")
    End Sub
8
  • But what is the issue ? What error message you are getting ? Commented Aug 27, 2020 at 13:30
  • Worksheets("VEA").Range("b2").Value is hard coded to b2? - as you go through the loop you'll forever write to that cell... Beyond that you need to describe what is happening and what you expec to happen with enought supporting information for us to understand your problem. Commented Aug 27, 2020 at 14:11
  • Mr Rich, It suppose that if it works it should give me the price (that is what i need), wich is under class 'product-price', but it don't. I just want this element, but if the problem would be the 'b2' it should bring it anyay on debbuging and also it bring me nothing Commented Aug 27, 2020 at 14:23
  • @ rahul rai, no error, just doesn't bring any data Commented Aug 27, 2020 at 14:25
  • It seems synchronization issue.You need provide some explicit wait before interaction the element.Set myproducts = bot.FindElementsByClass("datos-producto-container") Commented Aug 27, 2020 at 14:59

1 Answer 1

1

Issue is in this line :

Worksheets("VEA").Range("b2").Value = myproducts.FindElementsByClass("product-price").Text

Remember FindElements, returns a list of webelements rather than webelement. Instaead use the line you have used in if condition.

Worksheets("VEA").Range("b2").Value=myproduct.FindElementByClass("product-price").Text 

Note : With above line of code you will get your price, but it will come as $379 instead of $3.79. As there is no . in price on page. Better way to store price is :

Dim intValue = myproduct.FindElementByClass("product-price").Text
Dim decValue=   myproduct.findElementByXPath(".//div[@class='product-price']//span").Text
Worksheets("VEA").Range("b2").Value = Replace(intValue , decValue, "."&decValue)

Above will assign $3.79.

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for reply. Can't make it bring me the price data, i tried both method you gave me. The price is $92, no decimals today, i don't know if it could complicate and the first method maybe something is wrong on my code.
In the Edit you suggested i can still see you are using myproducts.FindElementByClass("product-price").Text instead of myproduct.FindElementByClass("product-price").Text. Please note here myproducts refer to a list here and myproduct to a webelement. Please try with exact code.
Dear Rahul. thank you for your help. I finally could do it with your suggestions. One thing i can't understand, there are some links that if you entry manual show a price, wich is the true price and entering by the scrypt it shows a very different price an if you try fast to inspect before the url get closed it come from the same div class, it happen only with some products, What could be happening? i will give you and example veadigital.com.ar/prod/109704/mandarina-promocion
@Marina Sure I will have a look and try to replicate your issue. Just one suggestions. If your original issue is resolved please accept and upvote as token of appreciation. Thanks and cheers !!!
@MarinaMontero Regrading your issue with manual url giving different rate and and automation script giving different. It should not be the case. As i have tried with URL veadigital.com.ar/prod/109704/mandarina-promocion and i got same rate in both case $15.99. For second issue where it is failing exactly as until unless these is some chnage in HTML DOM it should not fail. Also most important As a pratice on Stack overflow once original issue is resolved you should accept / upvote the answer**. For New problem you should start a new thread by posting a fresh question. Cheers!!!
|

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.