gpt4 book ai didi

selenium - 具有下载功能的 headless 浏览器测试?

转载 作者:行者123 更新时间:2023-12-04 12:31:11 31 4
gpt4 key购买 nike

我一直在寻找在 osx 中进行 headless 测试的解决方案。但我需要能够保存服务器返回的文件。

我已经测试了 selenium、phantomjs、casperjs 并研究了我可以在网上找到的任何东西。

他们都不支持下载。我错过了什么吗?是否有任何支持下载的 headless 浏览器/测试框架?

最佳答案

你可以做的是:

  • 开始 虚拟显示 (见 Xvfb)
  • 启动 Firefox首选项配置为 的浏览器自动保存 csv文件

  • 带有附加注释的 Python 中的工作示例(使用 pyvirtualdisplay xvfb 包装器):
    from os import getcwd
    import time

    from pyvirtualdisplay import Display
    from selenium import webdriver

    # start the virtual display
    display = Display(visible=0, size=(800, 600))
    display.start()

    # configure firefox profile to automatically save csv files in the current directory
    fp = webdriver.FirefoxProfile()
    fp.set_preference("browser.download.folderList", 2)
    fp.set_preference("browser.download.manager.showWhenStarting", False)
    fp.set_preference("browser.download.dir", getcwd())
    fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")

    browser = webdriver.Firefox(firefox_profile=fp)
    browser.get('http://www.nationale-loterij.be/nl/onze-spelen/lotto/resultaten')

    # check the option
    browser.find_element_by_id('corporatebody_3_corporategrid93961a8f9b424ed6bd0697df356d9483_1_rblType_0').click()

    # click the link
    browser.find_element_by_name('corporatebody_3$corporategrid93961a8f9b424ed6bd0697df356d9483_1$btnDownload').click()

    # hardcoded delay for waiting a file download (better check for the downloaded file to appear on the disk)
    time.sleep(2)

    # quit the browser
    browser.quit()

    # stop the display
    display.stop()

    也可以看看:
  • How do I run Selenium in Xvfb?
  • Access to file download dialog in Firefox
  • 关于selenium - 具有下载功能的 headless 浏览器测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27942322/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com