Search code examples
pythonseleniumselenium-webdriveryahoo-financecryptocurrency

Selenium Python - How to click on Time Period from Historical Data in Yahoo Finance using Selenium


I am having trouble trying to find the element of the time period. The next step i will do is to select the Max period and then download the data. I tried inspect element but could not find a suitable element for XPATH.

Here's my code

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

Solution

  • grabTimePeriod = driver.find_element_by_xpath("//span[contains(text(),'Time Period')]/../..//div/span")
    

    Explanation: the best way as for me is to find it by text with the help of xpath because using class names as in this element: <span class="C($linkColor) Fz(14px)">Apr 13, 2020 - Apr 13, 2021</span> is not stable.

    Also, import:

    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
    

    And wait for this locator before clicking.

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