如何获得百分比值?
How to get hold of the percentag value?
这是我从这个特定网站获取百分比的代码,但我总是会出错,而且使用新版本的 selenium 我真的不知所措。
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()
这是我得到的错误
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"}
谁能告诉我我做错了什么。或者在哪里查看文档。
那里有 3 个百分比。
您可以获得以下所有 3 个值
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)
您也不应该使用像 time.sleep(5)
这样的硬编码暂停,应该改用预期条件显式等待
这是我从这个特定网站获取百分比的代码,但我总是会出错,而且使用新版本的 selenium 我真的不知所措。
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()
这是我得到的错误
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"}
谁能告诉我我做错了什么。或者在哪里查看文档。
那里有 3 个百分比。
您可以获得以下所有 3 个值
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)
您也不应该使用像 time.sleep(5)
这样的硬编码暂停,应该改用预期条件显式等待