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?