0

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.

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
2652413782
  • 11
  • 2
  • It appears you didn't quite fully copy the code from pycharm. In the future, try to give as little information in the form of pictures, and copy and paste the error codes into your post. It makes it easier for others to help. – goalie1998 Jan 18 '21 at 20:17
  • Sorry,I will do it next time. – 2652413782 Jan 19 '21 at 04:17

1 Answers1

-1

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
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217