简体   繁体   English

使用python selenium键入文本后如何从搜索建议中获取价值?

[英]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.当您在https://finance.yahoo.com/的搜索栏中输入诸如苹果之类的内容时,会出现一个搜索建议菜单。 雅虎财经

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.',.....}例如 {'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.但它一直说 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:这是列表的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')

This is not the correct xpath:这不是正确的 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 python selenium 键入文本后如何从搜索建议中获取值? - How to get values from search suggestions after keying in text using python selenium? 如何使用python从硒中的禁用输入中获取文本值? - how to get text value from disabled input in selenium using python? 如何使用Selenium Python从duckduckgo的搜索结果中提取文本 - How to extract the text from the search results of duckduckgo using Selenium Python 如何使用 Selenium 和 Python 从自动建议中选择 select - How to select an option from the auto suggestions using Selenium and Python "使用 python 和 selenium 从 HTML 表中获取特定的文本值" - Get a specific text value from an HTML table using python and selenium 使用Python和Selenium Webdriver从textarea获取文本(无值属性) - Get text from textarea using Python & Selenium Webdriver (no value attribute) Python Selenium如何在跨度后从div获取文本 - Python Selenium how to get text from a div after a span 如何使用Python使用Selenium Webdriver获取此html中的一些文本值? - How to get the some text value in this html with Selenium Webdriver using Python? Selenium / Python使用Selenium扩展文本后如何获取全文? - Selenium/ Python How to get full text after expanding the text using Selenium? Python 硒测试。 如何从 Google 主页的搜索框中提取自动建议? - Python Selenium Testing. How can I extract the Auto Suggestions from search box on Google Home Page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM