Skip to content Skip to sidebar Skip to footer

How To Get Values From Search Suggestions After Keying In Text Using Python Selenium?

When you enter something for example apple into the search bar at https://finance.yahoo.com/ there is a search suggestions menu that appears. I am trying to get it to return a lis

Solution 1:

Made some changes to your script around waiting and xpaths. The result will be the suggested data in a pandas dataframe.

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

options=webdriver.ChromeOptions()
options.add_argument('start-maximized')

driver = webdriver.Chrome(options=options)
url = "https://finance.yahoo.com/"
driver.get(url)

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="yfin-usr-qry"]'))).send_keys('apple')

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div//*[contains(text(),'Symbols')]")))
web_elem_list = driver.find_elements_by_xpath(".//div[@data-test='search-assist-input-sugglst']/div/ul[1]/li/div")
results = pd.DataFrame()

for web_elem in web_elem_list:
    suggests=[]

    code=web_elem.find_element_by_xpath("./div/div").text
    suggests.append(code)

    name=web_elem.find_element_by_xpath("./div/div/following-sibling::div").text
    suggests.append(name)

    equity=web_elem.find_element_by_xpath("./div/following-sibling::div").text
    suggests.append(equity)

    hyperlink=f'https://finance.yahoo.com/quote/{code}?p={code}&.tsrc=fin-srch'
    suggests.append(hyperlink)

    results=results.append(pd.Series(suggests), ignore_index=True)

print(results)

driver.close()

Output:enter image description here

Solution 2:

This worked for me:

driver.find_elements_by_css_selector("ul[class=f470fc71] li[role=link][data-type=quotes]")
suggestions = [web_elem.get_attribute("title") for web_elem in web_elem_list]

output:

['Apple Inc.', 'Apple Hospitality REIT, Inc.', 'Apple Inc.', 'Apple Rush Company, Inc.', 'Apple Inc.', 'ApplePie Capital']

Post a Comment for "How To Get Values From Search Suggestions After Keying In Text Using Python Selenium?"