MarinaMontero
MarinaMontero

Reputation: 25

Extract Data with Selenium and VBA

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

Upvotes: 1

Views: 581

Answers (1)

rahul rai
rahul rai

Reputation: 2326

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.

Upvotes: 1

Related Questions