0
votes

I have 5 or 6 products under an anchor tag and in the same anchor tag, there is product name and price with distinct div. What I am trying to do compare the price of all products and click the link with the highest price.

My approach was:

 product = driver.find_elements_by_xpath('//div[@class="slug__Grid-vcgsbx-0 hPFNJV pb-24 my-4 md:pb-4"]')
    for individual in product:
        productlink = individual.find_elements_by_tag_name("a")
        for link in productlink:
            data['link']= link.get_attribute('href')
            print(link.get_attribute('href'))
            name = link.find_elements_by_xpath('//p[@class="Card___StyledP4-sc-1629dl9-4 fWEsJX text-sm text-gray-800"]')
            price = link.find_element_by_xpath('//p[@class="Card___StyledP2-sc-1629dl9-1 cFzjHk"]')
            for singlename in name:
                data["name"]=singlename.text
                print(singlename.text)
                for singleprice in price:
                    data["price"]=singleprice.text

I just scraped the value from the page but couldn't make logic on how should I do that. I have added a page link and an image (in case if you don't want to click the link you can see the photo).

Page Link:: problem page link

Images:: product frontend page

Image2:: product page inspect mode

1

1 Answers

0
votes

The prices are not in the same class so you will have to get them separately and then combine them. This is one way to do it:

This xpath will get the prices that have been reduced

salePrices = driver.find_elements_by_xpath("//p[contains(@class,'Card___StyledP2')]")

This xpath will get the standard prices

regularPrices = driver.find_elements_by_xpath("//p[contains(@class,'Card___StyledP3')]")

Then you will need to combine all the prices, remove the symbol and convert them to integers in order to compare them.

for p in salePrices:
prices.append(int(p.text.replace('৳','')))

for r in regularPrices:
    prices.append(int(r.text.replace('৳','')))

To find the largest, use max()

greatestPrice = max(prices)

To click on that element, convert back into a string

driver.find_element_by_xpath(f"//p[contains(normalize-space(.), '{str(greatestPrice)}')]").click()