Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用selenium python获取雅虎财经搜索自动建议结果?_Python_Selenium_Selenium Webdriver_Xpath_Webdriverwait - Fatal编程技术网

如何使用selenium python获取雅虎财经搜索自动建议结果?

如何使用selenium python获取雅虎财经搜索自动建议结果?,python,selenium,selenium-webdriver,xpath,webdriverwait,Python,Selenium,Selenium Webdriver,Xpath,Webdriverwait,我正在尝试使用selenium python自动搜索雅虎财经。当我输入一些单词时,一个建议会像谷歌建议一样弹出 我发现一个带有xpath的列表元素应该是yahoo提出的建议: //*[@id="search-assist-input"]/div[2]/ul 这个列表中似乎隐藏了建议内容,但它是不可见的,我的意思是,当我单击展开它时,它就消失了。我不知道firefox或chrome中是否有某种“总是展开节点”,但这些元素似乎很难触及。 我尝试获取此元素下的所有子元素,结果显示找不到任何元素:

我正在尝试使用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']/*")
如何从搜索框中获取这些自动建议?

https://finance.yahoo.com/
您必须诱导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
    
由于网站的源代码可能已经更改,我将@DebanjanB的答案调整为三点:

  • 单击此处接受Cookie/提交同意书
  • 搜索字段的Xpath(至少适用于德国/欧盟)
  • 建议列表的Xpath

  • 很好,我可以问你怎么知道它存储在li节点中?我应该如何使用firefox/chrome进行检查?@bot1那些
  • 标记是基于JS的标记,您可以随时拿出
    页面\u source
    来检查内容。作为一种快捷方式,我拍摄了一张快照,以识别子标记是
  • 标记:)好的,但当我单击展开ul节点时,它只是淡出。你是怎么拍快照的?我的意思是,你是不是在点击展开按钮后很快就开始了?@bot1我本来不应该走捷径:)让我们遵循最佳实践,即
    page\u source
    是的,我实际上刚刚意识到这正是我在另一个问题中寻找的。
    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)