簡體   English   中英

使用python selenium鍵入文本后如何從搜索建議中獲取價值?

[英]How to get value from search suggestions after keying in text using python selenium?

當您在https://finance.yahoo.com/的搜索欄中輸入諸如蘋果之類的內容時,會出現一個搜索建議菜單。 雅虎財經

我試圖讓它返回該下拉框中值的列表、字典或數據框。

例如 {'AAPL':'Apple Inc.', 'APLE':'Apple Hospitality REIT,Inc.',.....}

這是我的代碼,

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

但它一直說 TypeError: 'WebElement' object is not iterable。 我確定我已經選擇了列表。 我如何讓它按照我描述的方式工作?

這是列表的xpath

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

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

這不是正確的 xpath:

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

編寫一些腳本來填充預填充的建議,然后:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM