[英]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.