Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get hold of the percentag value?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading