简体   繁体   English

如何使用Selenium关闭Chrome的“下载”对话框(单击“取消”)

[英]How to dismiss Download dialog of Chrome with Selenium (by clicking Cancel)

I can automate the Download action in Chrome with WebDriver. 我可以使用WebDriver在Chrome中自动执行下载操作。 Below is my code to setup Chrome Driver: 以下是我设置Chrome驱动程序的代码:

ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromePrefs = new HashMap<>();
chromePrefs.put("download.default_directory", "/pathToDownloadFolder");
chromePrefs.put("download.prompt_for_download", false);
chromePrefs.put("download.directory_upgrade", true);
chromePrefs.put("safebrowsing.enabled", true);
options.setExperimentalOption("prefs", chromePrefs);
// create chrome driver
webDriver = new ChromeDriver(options);

The file will be saved in the pathToDownloadFolder. 该文件将保存在pathToDownloadFolder中。

But I don't want actually to save file. 但我不想实际保存文件。 My intention is to automate user's action: Click to download -> Download dialog shown up -> User clicks cancel. 我的目的是自动化用户的操作:点击下载 - >显示的下载对话框 - >用户点击取消。

Which option can mirror the Cancel button of Download Dialog? 哪个选项可以镜像下载对话框的取消按钮?

EDIT 编辑

I want to download, but not save, because I need to click download button in order to activate another button to click on, so saving file is not necessary, that's why I want to dismiss (or automate action user clicks Cancel when Download dialog apears) 我想下载,但不能保存,因为我需要点击下载按钮才能激活另一个按钮点击,所以不需要保存文件,这就是为什么我要解雇(或者自动化动作用户点击取消时下载对话框apears )

I looked into Chrome Options , but it seems it is not a possibility with current implementation. 我查看了Chrome选项 ,但目前的实施似乎不太可能。 Is there any workaround? 有没有解决方法?

To enable or to prompt the browser to ask the permission can be done by using below code, 要使用以下代码启用或提示浏览器询问权限,

chromePrefs.put("download.prompt_for_download", false);

I can see that you have used it. 我可以看到你使用过它。 Make it to true . 使其成为true

chromePrefs.put("download.prompt_for_download", true);

This will ask you to select the location for download. 这将要求您选择下载位置。 Since after clicking the download, in most cases the system window will open up and this cannot be handled using webdriver. 由于点击下载后,在大多数情况下系统窗口将打开,无法使用webdriver处理。 For this you have to make use of third party tools like Sikuli . 为此,你必须使用像Sikuli这样的第三方工具。

More information for Sikuli can be found here . 有关Sikuli的更多信息,请点击此处

I have not used Sikuli , but when I referred about your question. 我没有使用过Sikuli ,但是当我提到你的问题时。 I saw the example given in the above link. 我看到了上面链接中给出的例子。 May be that will help you. 可能会对你有所帮助。

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

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