Ryan
Ryan

Reputation: 3

Finding xpath for clicking and finding text

Error: NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//td[@class='C($primaryColor) W(51%)']"} (Session info: chrome=77.0.3865.120)

My Code is below:

from selenium import webdriver

from selenium.webdriver.common.keys import Keys


driver = webdriver.Chrome('/Users/ryanyee/Desktop/Python Code/Selenium/Launch 
Chrome/chromedriver')
driver.get('https://finance.yahoo.com')
search_box = driver.find_element_by_id('yfin-usr-qry')
search_box.send_keys('GOOG')
search_box.submit()

name = driver.find_element_by_xpath("//td[@class='C($primaryColor) W(51%)']").text()

My problem is that it throws an error when I try to scrape the text for name.

Also, I have trouble trying to click the button "Historical Data"

This is the website I am trying to scrape from 'https://finance.yahoo.com/quote/GOOG?p=GOOG'

Please let me know what I am doing wrong! I have been stuck for days!

Upvotes: 0

Views: 85

Answers (2)

frianH
frianH

Reputation: 7563

You need wait after submit.

search_box.submit()

name = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//td[@class='C($primaryColor) W(51%)']"))).text
print(name)

#this is for click Historical Data
driver.find_element_by_link_text("Historical Data").click()

Following import:

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

Upvotes: 0

ramya
ramya

Reputation: 31

Try Adding wait statement after search_box.submit()

from selenium.webdriver.support.ui import WebDriverWait

WebDriverWait(driver, 10).until(
    EC.presence_of_element_located(By.XPATH, "//td[@class='C($primaryColor) W(51%)']"))

Upvotes: 1

Related Questions