简体   繁体   中英

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

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