我正在尝试使用selenium python来自动搜索雅虎财经。当我输入一些单词时,建议会像谷歌建议一样弹出。
我发现带有xpath的列表元素应该是yahoo提出的建议:
//*[@id="search-assist-input"]/div[2]/ul
似乎建议内容隐藏在此列表中,但它是不可见的,我的意思是当我点击展开它时,它就会消失。我不知道firefox或chrome中是否存在某种“总是展开的节点”,但这些元素似乎很难达到。 我试图让所有的孩子都在这个元素下,它显示没有元素可以找到:
from chrome_driver.chrome import Chrome
driver = Chrome().get_driver()
driver.get('https://finance.yahoo.com/')
driver.find_elements_by_xpath("//div[@id='search-assist-input']/div/input")[0].send_keys('goog')
x = driver.find_elements_by_xpath("//div[@data-reactid='56']/ul[@data-reactid='57']/*")
如何从搜索框中获取这些自动建议?
答案 0 :(得分:1)
针对搜索文本提取自动建议,例如https://finance.yahoo.com/
的搜索框中的 GOOG ,您必须诱导 WebDriverWait ,以便自动建议可见您可以使用以下解决方案:
代码块:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\ChromeDriver\chromedriver_win32\chromedriver.exe')
driver.get('https://finance.yahoo.com/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='p']"))).send_keys("goog")
yahoo_fin_auto_suggestions = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//input[@name='p']//following::div[1]/ul//li")))
for item in yahoo_fin_auto_suggestions :
print(item.text)
控制台输出:
GOOG
Alphabet Inc.Equity - NASDAQ
GOOGL
Alphabet Inc.Equity - NASDAQ
GOOGL-USD.SW
AlphabetEquity - Swiss
GOOGL180518C01080000
GOOGL May 2018 call 1080.000Option - OPR
GOOG.MX
Alphabet Inc.Equity - Mexico
GOOG180525C01075000
GOOG May 2018 call 1075.000Option - OPR
GOOG180518C00720000
GOOG May 2018 call 720.000Option - OPR
GOOGL180518C01120000
GOOGL May 2018 call 1120.000Option - OPR
GOOGL.MX
Alphabet Inc.Equity - Mexico
GOOGL190621C01500000
GOOGL Jun 2019 call 1500.000Option - OPR
答案 1 :(得分:0)
由于https://finance.yahoo.com/网站的源代码可能已更改,因此我在三个方面调整了@DebanjanB的答案:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
#options.add_argument('headless') #optional for headless driver
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Program Files (x86)\Google\Chrome\Chromedriver\chromedriver.exe')
driver.get('https://finance.yahoo.com/')
driver.find_element_by_xpath("//button[@type='submit' and @value='agree']").click() #for cookie consent
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='yfin-usr-qry']"))).send_keys("goog")
yahoo_fin_auto_suggestions = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, '(//div[@class="_0ea0377c _4343c2a0 _50f34a35"])')))
for item in yahoo_fin_auto_suggestions:
print(item.text)