Selenium Python - 如何使用 Selenium 从 Yahoo Finance 中的历史数据中单击时间段
Selenium Python - How to click on Time Period from Historical Data in Yahoo Finance using Selenium
我在尝试查找时间段的元素时遇到问题。我下一步要做的是 select 最大周期,然后下载数据。
我试过 inspect element 但找不到适合 XPATH 的元素。
这是我的代码
enter code here
import selenium
import time
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities().CHROME
caps["pageLoadStrategy"] = "none" # Do not wait for full page load
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://finance.yahoo.com/quote/XRP-USD?p=XRP-USD")
time.sleep(2.9)
driver.execute_script("window.stop();")
clickHistorical = driver.find_element_by_xpath('//span[text()= "Historical Data"]')
clickHistorical.click()
time.sleep(3.9)
grabTimePeriod = driver.find_element_by_xpath("HOW TO FIND THE ELEMENT OF THE TIME PERIOD??").click()
grabTimePeriod = driver.find_element_by_xpath("//span[contains(text(),'Time Period')]/../..//div/span")
说明:对我来说最好的方法是在 xpath 的帮助下通过文本找到它,因为在此元素中使用 class 名称:<span class="C($linkColor) Fz(14px)">Apr 13, 2020 - Apr 13, 2021</span>
不稳定。
此外,导入:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By as By
并在点击之前等待这个定位器。
wait.until(EC.element_to_be_clickable(
(By.XPATH, "//span[contains(text(),'Time Period')]/../..//div/span")))
grabTimePeriod = driver.find_element_by_xpath("//span[contains(text(),'Time Period')]/../..//div/span")
grabTimePeriod.click()
我在尝试查找时间段的元素时遇到问题。我下一步要做的是 select 最大周期,然后下载数据。 我试过 inspect element 但找不到适合 XPATH 的元素。
这是我的代码
enter code here
import selenium
import time
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities().CHROME
caps["pageLoadStrategy"] = "none" # Do not wait for full page load
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://finance.yahoo.com/quote/XRP-USD?p=XRP-USD")
time.sleep(2.9)
driver.execute_script("window.stop();")
clickHistorical = driver.find_element_by_xpath('//span[text()= "Historical Data"]')
clickHistorical.click()
time.sleep(3.9)
grabTimePeriod = driver.find_element_by_xpath("HOW TO FIND THE ELEMENT OF THE TIME PERIOD??").click()
grabTimePeriod = driver.find_element_by_xpath("//span[contains(text(),'Time Period')]/../..//div/span")
说明:对我来说最好的方法是在 xpath 的帮助下通过文本找到它,因为在此元素中使用 class 名称:<span class="C($linkColor) Fz(14px)">Apr 13, 2020 - Apr 13, 2021</span>
不稳定。
此外,导入:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By as By
并在点击之前等待这个定位器。
wait.until(EC.element_to_be_clickable(
(By.XPATH, "//span[contains(text(),'Time Period')]/../..//div/span")))
grabTimePeriod = driver.find_element_by_xpath("//span[contains(text(),'Time Period')]/../..//div/span")
grabTimePeriod.click()