0

I want to get the price from the Amazon website.

How can I take the price number?

#---------------------------------------------------------------------------------
chrome_driver_path = r"C:\Users\user\Desktop\Development\chromedriver.exe"
service = Service('C:\Program Files\Chrome Driver\chromedriver.exe')

driver = webdriver.Chrome(service=service)

driver.get("https://www.amazon.com/Apple-MacBook-16-Inch-Storage-2-6GHz/dp/B08CZT64VP/ref=sr_1_4?crid=2T5BGI71K1C8W&keywords=macbook&qid=1689176236&sprefix=macboo%2Caps%2C357&sr=8-4")

price = driver.find_element(By.CLASS_NAME,"a-offscreen").get_attribute("aria-hidden")

print(price)
#---------------------------------------------------------------------------------

#output: None

Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
BusyBee
  • 3
  • 1

3 Answers3

0

There are multiple tags with the same class, due to which you may not be able to get the tag that you need. Also you were trying to get an attribute which the element doesn't even have.

Therefore change line

price = driver.find_element(By.CLASS_NAME,"a-offscreen").get_attribute("aria-hidden")

to

price = driver.find_element(By.CSS_SELECTOR,"span.a-price.a-text-price.a-size-medium span.a-offscreen").get_attribute("innerText")

Output:

$849.69
Zero
  • 1,807
  • 1
  • 6
  • 17
0

Your program returns None because the find_element() returns the first span (with the a-offscreen class), that does not have an aria-label; then by default if one was not found get_attribute("aria-label") returns None. If you want to find the price text use the .text attribute of the span object, eg price = driver.find_element(By.CLASS_NAME,"a-offscreen").text

You may refer to https://pythonexamples.org/python-selenium-find-element-by-css-selector/

0

To extract the text $845.69 ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use the following locator strategies:

  • Using CSS_SELECTOR and text attribute:

    driver.get("https://www.amazon.com/Apple-MacBook-16-Inch-Storage-2-6GHz/dp/B08CZT64VP/ref=sr_1_4?crid=2T5BGI71K1C8W&keywords=macbook&qid=1689176236&sprefix=macboo%2Caps%2C357&sr=8-4")
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div#apex_desktop div#corePrice_desktop table.a-lineitem span.apexPriceToPay[data-a-color='price']"))).text)
    
  • Console output:

    $845.69
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352