简体   繁体   中英

How to get value 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 list, dictionary or dataframe of the values in that drop down box.

For example {'AAPL':'Apple Inc.', 'APLE':'Apple Hospitality REIT,Inc.',.....}

Here is my code,

options = Options()
options.add_argument("--disable-gpu")

driver = webdriver.Chrome(executable_path=r'C:\Program Files\chromedriver\chromedriver.exe',options=options)

url = "https://finance.yahoo.com/"
driver.get(url)
time.sleep(0.5)

inputElement = driver.find_element_by_xpath('//*[@id="yfin-usr-qry"]')
inputElement.send_keys('apple')
time.sleep(0.5)

web_elem_list = driver.find_element_by_xpath('//*[@id="fin-srch-assist"]/div[1]/div/ul[1]')
suggests = [web_elem.text for web_elem in web_elem_list]

print(suggests)

driver.close()

but it keeps saying TypeError: 'WebElement' object is not iterable. I'm sure I've selected the list. How do I get it to work the way I described?

This is xpath for the list:

.//ul[@class='M(0)']/li/div/div/div

web_elem_list = driver.find_elements_by_xpath('.//ul[@class='M(0)']/li/div/div/div')

This is not the correct xpath:

//*[@id="fin-srch-assist"]/div[1]/div/ul[1]

script something to populate the prefilled suggestions, then:

stocks = driver.find_elements_by_css_selector("div.Ell.C(black).D(i).Va(tb)")
for x in stocks:
    print(x.text)

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