Handling “This type of file can harm your computer.” Windows Dialog Box In Chrome Browser

Hello Folks,

You might see below dialog box when you download any file in Chrome browser and it finds that it is harmful for your computer:

This is a window/browser popup which can not be inspected. You can suppress this warning message using ChromeOptions. You need to set “safebrowsing.enabled” as true.

Java Code:

[java]import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class ChangeDownloadDirOfChrome {

public static void main(String[] args) throws IOException {

// Setting chrome driver path
System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");

// Setting new download directory path
Map<String, Object> prefs = new HashMap<String, Object>();

prefs.put("profile.default_content_settings.popups", 0);

// Use File.separator as it will work on any OS
prefs.put("download.default_directory",
System.getProperty("user.dir") + File.separator + "externalFiles" + File.separator + "downloadFiles");

// suppressing "This type of file can harm your computer." popup
prefs.put("safebrowsing.enabled", "false");

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

// Adding capabilities to ChromeOptions
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);

// Printing set download directory
System.out.println(options.getExperimentalOption("prefs"));

// Launching browser with desired capabilities
ChromeDriver driver= new ChromeDriver(options);

// URL loading
driver.get("https://www.seleniumhq.org/download/");

// Click on download selenium server jar file
driver.findElement(By.xpath("//p[text()=’Download version ‘]/a")).click();

}
}
[/java]

Note:
Above code may or may not work always with all chrome versions. Above code is tested with Chrome Version 68. I will keep updating posts, if options are changed.

If you like my posts, let me know in comments.
If you do not like, let me your feedback in comments.
If you are neutral, stay with me.

#HappySelenium
#HappyLearning

7 thoughts on “Handling “This type of file can harm your computer.” Windows Dialog Box In Chrome Browser

  1. Hi,

    I am using above code for the chrome version 69
    But still the pop up gets appeared.

    please let me know how to bypass this pop up for the chrome version 69
    I have googled lot but i didn’t find any solution.

Leave a Reply to Preetha Cancel reply

Your email address will not be published. Required fields are marked *