Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I can automate the Download action in Chrome with WebDriver. Below is my code to setup Chrome Driver:

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.

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)

I looked into Chrome Options, but it seems it is not a possibility with current implementation. Is there any workaround?

like image 453
Ragnarsson Avatar asked May 02 '18 13:05

Ragnarsson


1 Answers

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.

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. For this you have to make use of third party tools like Sikuli.

More information for Sikuli can be found here.

I have not used Sikuli , but when I referred about your question. I saw the example given in the above link. May be that will help you.

like image 67
kripindas Avatar answered Sep 16 '22 13:09

kripindas