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:

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();
		

	}
}

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

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

5 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

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.
%d bloggers like this: