首页 文章

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

提问于
浏览
6

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

我测试了selenium,phantomjs,casperjs,并研究了我能在网上找到的任何东西 .

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

3 回答

  • 4

    你能做的是:

    • 开始 virtual display (见Xvfb

    • 启动 Firefox 浏览器,首选项配置为 automatically save csv files

    python中的工作示例以及其他注释(使用pyvirtualdisplay xvfb wrapper):

    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()
    

    也可以看看:

  • -1

    我使用OSX selenium wget命令来执行下载 .

    这是一个代码示例:

    new_driver = webdriver.Firefox()
    new_driver.get(url)
    for element in new_driver.find_elements_by_tag_name('img'):
        os.system('wget ' + element.get_attribute('src').rstrip('\n'))
    
  • 1

    Awesomium是一个无头浏览器,它有下载事件处理程序的下载管理器 . Here是文档的链接 .

相关问题