如何在使用python selenium键入文本后从搜索建议中获取值?
当您在搜索栏中输入某个内容(例如apple)时,会出现一个搜索建议菜单 我试图让它返回下拉框中值的列表、字典或数据框 比如说如何在使用python selenium键入文本后从搜索建议中获取值?,python,html,css,selenium,Python,Html,Css,Selenium,当您在搜索栏中输入某个内容(例如apple)时,会出现一个搜索建议菜单 我试图让它返回下拉框中值的列表、字典或数据框 比如说 {'AAPL':['Apple Inc.','Equity - NMS','https://finance.yahoo.com/quote/AAPL?p=AAPL&.tsrc=fin-srch'], 'AAPL.BA':['Apple Inc.','Equity - BUE','https://finance.yahoo.com/quote/AAPL.BA?p
{'AAPL':['Apple Inc.','Equity - NMS','https://finance.yahoo.com/quote/AAPL?p=AAPL&.tsrc=fin-srch'],
'AAPL.BA':['Apple Inc.','Equity - BUE','https://finance.yahoo.com/quote/AAPL.BA?p=AAPL.BA&.tsrc=fin-srch'],
.....}
或
最后一个值是单击链接时的超链接
这是我到目前为止的代码
options = Options()
driver = webdriver.Chrome(executable_path=r'C:\Program Files\chromedriver\chromedriver.exe',options=options)
url = "https://finance.yahoo.com/"
driver.get(url)
time.sleep(2)
inputElement = driver.find_element_by_xpath('//*[@id="yfin-usr-qry"]')
inputElement.send_keys('apple')
time.sleep(2)
web_elem_list = driver.find_elements_by_xpath(".//ul[@class='M(0)']/li/div/div")
suggests = [web_elem.text for web_elem in web_elem_list]
print(suggests)
driver.close()
但是输出结果总是空的,我似乎找不到建议框中的元素
我还尝试使用web\u elem\u list=driver.find\u elements\u by\u xpath(“.//ul[@class='f470fc71']/li/div/div”)
但它没有任何价值
我怎么,
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]
输出:
['Apple Inc.', 'Apple Hospitality REIT, Inc.', 'Apple Inc.', 'Apple Rush Company, Inc.', 'Apple Inc.', 'ApplePie Capital']
在等待和XPath方面对脚本进行了一些更改。结果将是熊猫数据框中的建议数据
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()
输出:
您正在搜索的元素在iframe中。搜索框不在iframe中,但结果在iframe中?我可以看到selenium将值键入到SearchBox中,我看不到树顶部的iframe,你能给我看一下吗?“但输出一直是空的”-不太确定在你这方面是否有其他问题,但我能够运行你的代码并获得匹配行的列表。我注意到的唯一一件事是你有
web\u elem\u list=driver.find\u element\u by\u xpath(“.//ul[@M(0)]/li/div/div/div/div”)。
当正确的是web\u elem\u list=driver.find\u elementS\u by\u xpath(我想这只是问题中的一个输入错误?)。好的,我是说elementS,这是一个输入错误,我更新了我的问题。它仍然不工作,我得到一个空列表是的,它工作,但我只得到标题,其他值呢?嘿,它不工作了,首先,类M(0)不再存在于页面上,我使用f470fc71代替,其次它说没有这样的元素:无法找到元素:{“方法”:“xpath”,“选择器”:“/div”},还有,超链接有可能吗?更新了web元素列表
xpath,这样您就不必依赖类了。另外(重新)添加了ChromeOptions以启动浏览器最大化。嘿,所以我尝试了它,出于某种原因,它又变成了空白,我认为这与您尝试再次搜索时的./div有关。您是否已将此部分添加到脚本中options=webdriver.ChromeOptions()options.add_参数('start-maximized')
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()