简体   繁体   English

如何使用远程Firefox Webdriver下载文件?

[英]How to download file using remote Firefox webdriver?

I've tried to adapt several existing solutions ( 1 , 2 ) to the remote Firefox webdriver running in a selenium/standalone-firefox Docker container : 我试着去适应现有的几种解决方案( 12 )在一个运行的webdriver 远程火狐硒/独立Firefox的码头工人容器

options = Options()
options.set_preference('browser.download.dir', '/src/app/output')
options.set_preference('browser.download.folderList', 2)
options.set_preference('browser.download.manager.showWhenStarting', False)
options.set_preference('browser.helperApps.alwaysAsk.force', False)
options.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf')
options.set_preference('pdfjs.disabled', True)
options.set_preference('pdfjs.enabledCache.state', False)
options.set_preference('plugin.disable_full_page_plugin_for_types', False)

cls.driver = webdriver.Remote(
    command_executor='http://selenium:4444/wd/hub',
    desired_capabilities={'browserName': 'firefox', 'acceptInsecureCerts': True},
    options=options
)

Navigating and clicking the relevant download button works fine, but the file never appears in the download directory. 导航并单击相关的下载按钮可以正常工作,但是该文件永远不会出现在下载目录中。 I've verified everything I can think of: 我已经验证了我能想到的一切:

  • The user in the Selenium container can create files in /src/app/output and those files are visible in the host OS. Selenium容器中的用户可以在/ src / app / output中创建文件,并且这些文件在主机OS中可见。
  • I can download the file successfully using my desktop browser. 我可以使用桌面浏览器成功下载文件。
  • The response content type is application/pdf . 响应内容类型为application/pdf

What am I missing? 我想念什么?

It turned out other changes done while researching this were resulting in the server returning a text/plain document rather than a PDF file. 事实证明,在进行研究时还进行了其他更改,导致服务器返回了文本/普通文档而不是PDF文件。 For reference, this is the simplest set of options I could get to work: 作为参考,这是我可以使用的最简单的选项集:

options.set_preference('browser.download.dir', DOWNLOAD_DIRECTORY)
options.set_preference('browser.download.folderList', 2)
options.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf')
options.set_preference('pdfjs.disabled', True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM