小编典典

具有下载功能的无头浏览器测试?

selenium

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

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

他们都不支持下载。我错过了什么吗?有没有无头的浏览器/测试框架支持下载?


阅读 350

收藏
2020-06-26

共1个答案

小编典典

您可以做的是:

  • 启动 虚拟显示 (请参阅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()
2020-06-26