[英]Selenium Python - How to click on Time Period from Historical Data in Yahoo Finance using Selenium
[英]How to click a button On Yahoo Finance using Selenium
我試圖自動從yahoo財務發現的財務報告中提取“季度”數據,但找不到解決方法。 我以為可以在財務頁面( https://finance.yahoo.com/quote/AAPL/financials?p=AAPL )上單擊“季度按鈕”,但是下面的代碼不起作用。 (什么都沒發生)請您更正代碼嗎?
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome('c:/chromedriver/chromedriver.exe')
ticker_list = ["AAPL"]
for ticker in ticker_list:
url = "https://finance.yahoo.com/quote/" + ticker + "/financials?p=" + ticker
driver.get(url)
wait = WebDriverWait(driver, 3600)
wait.until(EC.presence_of_element_located((By.XPATH, '//button[text()="Quarterly"]')))
driver.find_element_by_xpath('//button[text()="Quarterly"]').click
“季度”文本是button元素內span
標簽的一部分,因此未找到。 如果您將XPath更改為//span[text()="Quarterly"]
則對我有用
要單擊文本為“ 季度”的元素,您需要誘導WebDriverWait使所需元素可單擊,並且可以使用以下解決方案:
代碼塊:
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC options = Options() options.add_argument("start-maximized") driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\\Utility\\BrowserDrivers\\chromedriver.exe') ticker_list = ["AAPL"] for ticker in ticker_list: url = "https://finance.yahoo.com/quote/{}/financials?p={}".format(ticker, ticker) driver.get(url) WebDriverWait(driver, 3600).until(EC.element_to_be_clickable((By.XPATH, "//section[@data-test='qsp-financial']//span[text()='Quarterly']"))).click()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.