Search code examples
pythonselenium-webdriverxpathcss-selectorsyahoo-finance

How to locate the time choosing button in Yahoo.finance?


I want to locate the time choosing button with in Yahoo Stock History page:

Snapshot of the element: ![enter image description here][2]

but the python code I write by xpath is unable to make it.The code is shown as follow:

WebDriverWait(browser, 180).until(EC.presence_of_element_located((By.XPATH, '//svg[@class="Va(m)! Mstart(8px) Stk($linkColor) Fill($linkColor) dateRangeBtn:h_Fill($linkActiveColor) 
button1 = browser.find_element(By.XPATH,'//svg[@class="Va(m)! Mstart(8px) Stk($linkColor) Fill($linkColor) dateRangeBtn:h_Fill($linkActiveColor) dateRangeBtn:h_Stk($linkActiveColor) W(8px) H(8px) Cur(p)"]')

But it will show the errors as follow: enter image description here or enter image description here Please give me some help or suggestions.I have no idea why it can not be found.


Solution

  • The element is a dynamic element. So to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      driver.get("https://finance.yahoo.com/quote/AAPL/history?p=AAPL")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "section[data-test=qsp-historical] div[data-test=dropdown] > div > span"))).click()
      
    • Using XPATH:

      driver.get("https://finance.yahoo.com/quote/AAPL/history?p=AAPL")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//section[@data-test='qsp-historical']//div[@data-test='dropdown']/div/span"))).click()
      
    • Note: You have to add the following imports :

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC