0

Hi i just started studying selenium and i wanted to create a program that would tell me the price of an amazon page. However I just can't go on. Where am I doing wrong?

This is my code

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By




driver=webdriver.Chrome()
url = 'https://www.amazon.com/dp/B00CDD2GU4'
driver.get(url)

try:
   price_parent = item.find('span','a-price')
   price = price_parent.find('span','a-offscreen').text.replace('$','')
except Exception as e:
   price_parent = '0'
   price = '0'



print(price)

I also used this code that I found online

driver=webdriver.Chrome()
url = 'https://www.amazon.com/dp/B00CDD2GU4'
driver.get(url)
price = driver.find_element(By.CSS_SELECTOR,"span.a-price.a-text-price.a-size-medium span.a-offscreen").get_attribute("innerText")
print(price)

But I get this:

PS C:\Users\mrcdi> & C:/Users/mrcdi/AppData/Local/Microsoft/WindowsApps/python3.11.exe "c:/Users/mrcdi/Desktop/Amazon Seller/Programma/prova.py"

DevTools listening on ws://127.0.0.1:64657/devtools/browser/fe6d839b-0f11-4f55-a192-9052f838a98d
Traceback (most recent call last):
  File "c:\Users\mrcdi\Desktop\Amazon Seller\Programma\prova.py", line 11, in <module>
    price = driver.find_element(By.CSS_SELECTOR,"span.a-price.a-text-price.a-size-medium span.a-offscreen").get_attribute("innerText")
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\mrcdi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 740, in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\mrcdi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 346, in execute
    self.error_handler.check_response(response)
  File "C:\Users\mrcdi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"span.a-price.a-text-price.a-size-medium span.a-offscreen"}
  (Session info: chrome=115.0.5790.102); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Stacktrace:
Backtrace:
        GetHandleVerifier [0x00ABA813+48355]
        (No symbol) [0x00A4C4B1]
        (No symbol) [0x00955358]
        (No symbol) [0x009809A5]
        (No symbol) [0x00980B3B]
        (No symbol) [0x009AE232]
        (No symbol) [0x0099A784]
        (No symbol) [0x009AC922]
        (No symbol) [0x0099A536]
        (No symbol) [0x009782DC]
        (No symbol) [0x009793DD]
        GetHandleVerifier [0x00D1AABD+2539405]
        GetHandleVerifier [0x00D5A78F+2800735]
        GetHandleVerifier [0x00D5456C+2775612]
        GetHandleVerifier [0x00B451E0+616112]
        (No symbol) [0x00A55F8C]
        (No symbol) [0x00A52328]
        (No symbol) [0x00A5240B]
        (No symbol) [0x00A44FF7]
        BaseThreadInitThunk [0x762900C9+25]
        RtlGetAppContainerNamedObjectPath [0x77DA7B1E+286]
        RtlGetAppContainerNamedObjectPath [0x77DA7AEE+238]

I am new to selenium so please can you help me?

This is a screen enter image description here

1 Answers1

1

The element with the price text is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following locator strategies:

    • Using CSS_SELECTOR:

      driver.get("https://www.amazon.com/dp/B00CDD2GU4")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#ape_Detail_desktop-detail-ilm_desktop_iframe")))
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span[data-testid='price']"))).text)
      
    • Using XPATH:

      driver.get("https://www.amazon.com/dp/B00CDD2GU4")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='ape_Detail_desktop-detail-ilm_desktop_iframe']")))
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@data-testid='price']"))).text)
      
  • 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
    

Reference

You can find a couple of relevant discussions in:


Update

Given the HTML:

html

you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.a-offscreen"))).text)
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='a-offscreen']"))).text)
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • So my code looks now like this """ driver.get("https://www.amazon.com/dp/B00CDD2GU4") WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#ape_Detail_desktop-detail-ilm_desktop_iframe"))) print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span[data-testid='price']"))).text) """ But I dont get the prices. My output is always: DevTools listening on ws://127.0.0.1:50337/devtools/browser/2b86f271-48e0-4cd0-94de-95f8790a3ad7 $ 47 73 – Marco Di Giacomo Jul 21 '23 at 21:53
  • @MarcoDiGiacomo Watch that`$ 47 73` , this is the price, right? You need micro adjustments using python liabries. – undetected Selenium Jul 21 '23 at 21:55
  • It is not, the price should be 89.90 – Marco Di Giacomo Jul 21 '23 at 21:56
  • where is that price on the screen? I don't find any price as **89.90** on that page. – undetected Selenium Jul 21 '23 at 21:57
  • I have attached a screenshot to my post. now I restarted the program and it gives me a different number: 159 99 Maybe you dont find the same price because of your location. I am located in Germany – Marco Di Giacomo Jul 21 '23 at 21:59
  • @MarcoDiGiacomo The segment containing the price isn't vivible from APAC region. Checkout the answer update and let me know the status. – undetected Selenium Jul 21 '23 at 22:07
  • Do you want to move the discussion to chat? Because the errors are too long to write them as a comment @undetected Selenium – Marco Di Giacomo Jul 21 '23 at 22:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/254615/discussion-between-marco-di-giacomo-and-undetected-selenium). – Marco Di Giacomo Jul 21 '23 at 22:13
  • Let's discuss the issue in [Selenium](https://chat.stackoverflow.com/rooms/223360/selenium) room. – undetected Selenium Jul 21 '23 at 22:17
  • Check this discussion [_**NoSuchElementException**_](https://stackoverflow.com/a/47995294/7429447) – undetected Selenium Jul 21 '23 at 22:35