如何使用 Selenium Webdriver 下载任何文件并将其保存到所需位置 [英] How to download any file and save it to the desired location using Selenium Webdriver

查看:80
本文介绍了如何使用 Selenium Webdriver 下载任何文件并将其保存到所需位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用下面给出的 Selenium Webdriver 执行以下任务.

I have to perform following task using Selenium Webdriver given below.

  1. 点击任何链接/按钮开始下载任何文件(文件类型可以是任何图像、pdf、jar 等)
  2. 如果出现,请单击弹出窗口中的保存"(例如,在 http://selenium.googlecode.com/files/selenium-server-standalone-2.33.0.jar)
  3. 提供保存该文件的所需位置.

谁能分享一下,我们如何使用 Java 实现这一点?

Can anyone share, how can we implement this using Java?

推荐答案

您将无法访问保存对话框.这是由操作系统控制的.您真正能够做的唯一一件事就是为浏览器设置默认下载位置并允许它自动下载文件.然后用Java检查文件.

You won't be able to access the save dialog box. That's controlled by the OS. The only thing you're really going to be able to do is set the default download location for the browser and allow it to automatically download the files. Then check the file in Java.

您应该从 这个答案access-to-file-download-dialog-in-firefox">这个以前的问题.基本上,在设置您的 Firefox 配置文件时,您添加一个调用以将属性 browser.helperApps.neverAsk.saveToDisk 设置为以逗号分隔的 MIME 类型列表,以便始终下载:

You should check this answer from this previous SO question. Basically when setting up your Firefox profile you add a call to set the property browser.helperApps.neverAsk.saveToDisk to a comma separated list of MIME types to always download:

firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");

请参阅关于 About:configs 的这篇 Mozilla 知识库常见问题解答文章.

See this Mozilla KB FAQ article on About:configs.

更新现在看起来这可能是可能的 在另一个问题中看到这个答案

UPDATE It looks like this may now be possible see this answer in another question

这篇关于如何使用 Selenium Webdriver 下载任何文件并将其保存到所需位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆