0

I am Test Automation Engineer, working with Selenium and Java. I am developing selenium automation tests but I have a problem with downloading something from the software I am working on. Chrome asks me the security question in order to download (Keep or Discard) but in selenium, as you know I cant click the chrome window. So I did try a few methods to get rid of this problem in chrome but I couldn't find a solution and this is important for me to continue to develop automation, that's why I wanted to ask if You know a solution to this issue. I download the XML file or cer file, I get a 'This type of file can harm your computer' pop up. I want to disable this pop up using selenium chromedriver and I want these types of files to be downloaded always. How can this be done?

Chrome version:80.0 Selenium version: 3.8.1 Chromedriver version: 2.42

I found these codes but cap object in DesiredCapabilities doesn't work with Chromedriver anymore;

HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
chromePrefs.put("safebrowsing.enabled", "true"); 
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap); //this driver doesnt work anymore 

I hope to get a reply from you. Thank you.

  • 1
    Could you add an example website – PDHide Jan 6 at 10:00
  • the website I am working is in a closed network so I cant give an example but I amtrying to download .cer or .XML file. Only in chromedriver running tests, security question is asked , without chromedriver test, in the website I can download files without being asked about downloading question by manuel. – M S Jan 6 at 10:59
  • ANy website that creates similar issue like you are facing could help in giving correct answer – PDHide Jan 6 at 11:02
  • "This type of file can harm your computer. Do you want to keep ...cer anyway?" is the question is being asked while chromedriver executes test, but when ı download, it directly download. Is the any solutions for this problem I could not find anything.Thank you replying btw. – M S Jan 6 at 11:22
  • In your browser you could have disabled safe browsing where as when it launched from selenium it launch with safe browsing enabled. check with chrome://settings/security?search=safe – Mohamed Sulaimaan Sheriff Jan 6 at 11:35
1
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("safebrowsing.enabled", true);
prefs.put("profile.default_content_settings.popups", 0);
options.setExperimentalOption("prefs", prefs);
System.setProperty("webdriver.chrome.driver", "C:\\Users\\prave\\Downloads\\chromedriver.exe");
ChromeDriver driver=new ChromeDriver(options);
WebDriverWait wait = new WebDriverWait (driver, 20);

driver.get("http://www.landxmlproject.org/file-cabinet");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='MntnRoad.xml']//following::span[1]//a[text()='Download']"))).click();

Just use : setExperimentalOptions , safebrowsing.enabled and ("profile.default_content_settings.popups", 0);

You can use:

 options.setAcceptInsecureCerts(true);

if you want to accept insecure cert

0

To hide the warning This type of file can harm your computer and enable downloading of xml type of files using Chrome/ChromeDriver you need to:

  • Add the preferences:

    • download.default_directory
    • download.prompt_for_download
    • download.extensions_to_open
    • safebrowsing.enabled
  • As well as add the following arguments to whilelist:

    • --safebrowsing-disable-download-protection
    • safebrowsing-disable-extension-blacklist
  • Code Block:

    System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
    Map<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("download.default_directory", "C:/Utility/Downloads/");
    prefs.put("download.prompt_for_download", false);
    prefs.put("download.extensions_to_open", "application/xml");
    prefs.put("safebrowsing.enabled", true);
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("prefs", prefs);
    options.addArguments("start-maximized");
    options.addArguments("--safebrowsing-disable-download-protection");
    options.addArguments("safebrowsing-disable-extension-blacklist");
    WebDriver driver =  new ChromeDriver(options); 
    driver.get("http://www.website.com");
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.