Unable to extract content using selenium

Advertisements

I’m unable to print price of the selenium object using class attribute extracted from the page. Using the same class I can extract from some products, but not for all. What is the mistake I’m doing here?

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

driver = webdriver.Chrome(options=chrome_options)

driver.get('https://www.amazon.in/Nothing-Phone-Black-128-RAM/dp/B0BKZVF7VV/ref=sr_1_2?crid=2V22OHL4YXGHF&keywords=nothing+phone+1&qid=1678089940&sprefix=nothing%2Caps%2C209&sr=8-2')

price_element = driver.find_element(By.CLASS_NAME, "a-offscreen")
print(price_element.text)

driver.quit()

I was expecting the print statement should provide the price of the product.

>Solution :

This selector

price_element = driver.find_element(By.CLASS_NAME, "a-offscreen")

selects multiple elements.

To get precisely price of the main product try using this

price_element = driver.find_element(By.ID, "corePriceDisplay_desktop_feature_div")

Leave a ReplyCancel reply