简体   繁体   中英

How to click a button On Yahoo Finance using Selenium

I was trying automatically pull "quarterly" data from the financial reports found on yahoo finance and could not find a way to do it. I thought I could click "the quarterly button" from the financial page ( https://finance.yahoo.com/quote/AAPL/financials?p=AAPL ) but below code didn't work. (Nothing happen) Would you please correct the code?

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

The "Quarterly" text is part of a span tag within the button element, so isn't found. If you change your XPath to //span[text()="Quarterly"] instead then that works for me

To click on the element with text as Quarterly you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:

  • Code Block:

     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() 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM