this is my code to get the percentage from this particular website, but I always get errors and with the new version of selenium I’m really overwhelmed.
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
driver = webdriver.Chrome('D:/Development/chromedriver')
driver.get("https://metf.finance/#/dashboard")
time.sleep(5)
percentage = driver.find_element(by=By.CSS_SELECTOR, value='bond-discounts a')
print(percentage.text)
driver.quit()
This is the Error I get
File "C:\Users\David\PycharmProjects\METF ALARM\main.py", line 10, in <module>
percentage = driver.find_element(by=By.CSS_SELECTOR, value='bond-discounts a')
File "C:\Users\David\PycharmProjects\METF ALARM\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1248, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\David\PycharmProjects\METF ALARM\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 425, in execute
self.error_handler.check_response(response)
File "C:\Users\David\PycharmProjects\METF ALARM\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, 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":"bond-discounts a"}
Can someone tell me what I'm doing wrong. Or where do look in the docs.
CodePudding user response:
There are 3 percentages there.
You can get all the 3 values as following
driver.get("https://metf.finance/#/dashboard")
time.sleep(5)
percentages = driver.find_elements(By.XPATH, "//div[@class='bond-discounts']//span")
for percentage in percentages:
print(percentage.text)
You also should not use hardcoded pauses like time.sleep(5)
, Expected Conditions explicit waits should be used instead