At times, we need to visit a page in our application that automatically starts downloading the file which is as per the feature of that page. But we’re navigating to this page (A) only to further navigate to the other page (B) by clicking on some link given on this page (A) which redirects us to page (B) but at the same time, we don’t want to download the file on visiting this page (A) as it’s not required every time while running our Selenium automation script.
Did you know there is a preference option “download_restrictions” that can help us achieve this?
Let’s say A is this: http://selenium-release.storage.googleapis.com/2.41/selenium-java-2.41.0.zip
HashMap<String, Object> prefs = new HashMap<String, Object>();
prefs.put(“download_restrictions”, 3);
ChromeOptions opt = new ChromeOptions();
opt.setExperimentalOption(“prefs”, prefs);
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(opt);
driver.get(“http://selenium-release.storage.googleapis.com/2.41/selenium-java-2.41.0.zip“);
Note* You can specify the type of downloads that Google Chrome will block, depending on how secure they are: https://support.google.com/chrome/a/answer/7579271?hl=en