4

How can I get rid of this keep/discard notification while downloading files via python selenium chromedriver?

I've tried with the following but could not succeed:

chromeOptions = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chromeOptions.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(chrome_options=chromeOptions)

EDIT: It seems I've found out another website having such ".eml" file which throws the same notification upon clicking on that link ending with .eml.

Example website link

I'm trying with the below approach:

from selenium import webdriver

url = "https://www.online-convert.com/file-format/eml"

dirf = r"C:\Users\WCS\Desktop\emlfolder"

def download_file(link):
    driver.get(link)
    driver.find_element_by_css_selector("a[href$='example.eml']").click()

if __name__ == '__main__':
    chromeOptions = webdriver.ChromeOptions()
    prefs = {'download.default_directory' : dirf}
    chromeOptions.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(chrome_options=chromeOptions)
    download_file(url)

The notification exactly looks like the image below:

enter image description here

Btw, I'm initiating click on that link to download only because the site I was experimenting with doesn't have any true url connected to that ".eml" files to navigate. Turn out that navigating to that ".eml" link lead to the same notification as well.

Community
  • 1
  • 1
robots.txt
  • 96
  • 2
  • 10
  • 36
  • We can’t help you without being able to analyse the site ourselves. – SimonF Feb 02 '19 at 15:42
  • https://windowsreport.com/type-of-file-can-harm-computer/ alleges that enabling "ask where to save" will get rid of this particular alert. [This](/questions/49303180/this-type-of-file-can-harm-your-computer-mac-no-keep-option-no-keep-option) seems to concur. – tripleee Feb 04 '19 at 05:16
  • I've explicitly defined the location of that download directory within the script like `prefs = {'download.default_directory' : some_location}` but still having the same notification @tripleee. – robots.txt Feb 04 '19 at 05:27
  • That sounds distinct from the workaround but IDK. – tripleee Feb 04 '19 at 05:44
  • Now you can check @SimonF. Thanks. – robots.txt Feb 04 '19 at 05:50
  • I don't get that notification, it possibly a windows thing and has nothing to do with your code – SimonF Feb 04 '19 at 09:42

7 Answers7

6

You need to specify the file extension you want to download

prefs = {
    'download.default_directory': dirf,
    'download.prompt_for_download': False,
    'download.extensions_to_open': 'eml',
    'safebrowsing.enabled': False
}

options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(chrome_options=options)
Guy
  • 46,488
  • 10
  • 44
  • 88
  • When I tried your suggestion within my script, It did download the eml file and finally led to open an outlook pop up window. However, today when I try again, It worked flawlessly. Could you tell me why? Gonna award the bounty soon. Thanks. – robots.txt Feb 10 '19 at 14:43
  • @robots.txt Did it ask you to choose a program to open thefile? Windows ask this the first time you open new type of file and save the option. – Guy Feb 10 '19 at 15:20
  • I tried the above for xml but does not work The chrome driver version is the latest –  Mar 28 '19 at 17:30
  • In my case `'safebrowsing.enabled': True` helped. – marcin Mar 17 '21 at 15:36
2

That's popup related to safe browsing. try

  chromeOptions = webdriver.ChromeOptions()
  prefs = {'safebrowsing.enabled': 'false'}
  chromeOptions.add_experimental_option("prefs", prefs)
  driver = webdriver.Chrome(chrome_options=chromeOptions)
murali selenium
  • 3,847
  • 2
  • 11
  • 20
0

According to this: How to disable 'This type of file can harm your computer' pop up you will need multiple options:

The accepted answer stopped working after a recent update of Chrome. Now you need to use the --safebrowsing-disable-extension-blacklist and --safebrowsing-disable-download-protection command-line switches.

But whenever someone at Google has second thoughts about security issues related to an option, they will modify the behaviour in the next version of Chrome. I am using Chrome 72 and the above mentioned options do not disable the notification anymore.

The short version: Do not try to disable security measures. Malware authors are trained to do that and any good browser developer seems to think "better safe than sorry".

If you really need just a download solution, you can use the requests module and download without chrome:

from selenium import webdriver
import requests

url = "https://www.online-convert.com/file-format/eml"

dirf = r"C:\Users\WCS\Desktop\emlfolder"

def download_file(link):
    driver.get(link)
    linkElement = driver.find_element_by_css_selector("a[href$='example.eml']")
    r = requests.get(linkElement.get_attribute('href'))
    file = open("C:\Users\WCS\Desktop\emlfolder\example.eml", 'wb')
    file.write(r.content)
    file.close()

if __name__ == '__main__':
    chromeOptions = webdriver.ChromeOptions()
    prefs = {'download.default_directory' : dirf}
    chromeOptions.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(chrome_options=chromeOptions)
    download_file(url)
    driver.quit()
Jens Dibbern
  • 1,434
  • 2
  • 13
  • 20
  • There is no true url connected to each download links. So using requests module is not a choice. I followed your suggestion to get rid of that warning/notification. However, that didn't work out. Thanks. – robots.txt Feb 06 '19 at 07:18
  • You can build your own url by combining the base url of the page and the relative link. But these command line parameters do nothing in the latest Chromium source code. I checked it online. – Jens Dibbern Feb 06 '19 at 18:00
0

You could try and add the website to the trusted sites list of chrome, If i understand your code, it uses your install of chrome, which would mean that if you were to change settings in chrome, the python module would use them.

Chrome

Click the 3 horizontal lines icon on the far right of the Address bar.

Click on Settings, scroll to the bottom and click the Show Advanced Settings link.

Click on Change proxy settings.

Click the Security tab > Trusted Sites icon, then click Sites.

Enter the URL of your Trusted Site, then click Add.

Click Close > OK.

That One
  • 581
  • 2
  • 6
  • 17
0

I did tried google many site/forums and found the below code, but it's still not working in Chrome 72. please let me know if you find any better solution for this issue.

System.setProperty("webdriver.chrome.driver", "chromedriver.exe file path")
String downloadFilepath = "C:\\Downloads";
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", "false"); 
chromePrefs.put("download.prompt_for_download", "false");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("--safebrowsing-disable-download-protection");
options.addArguments("--safebrowsing-disable-extension-blacklist");
options.addArguments("disable-extensions");
options.addArguments("test-type");
options.addArguments("start-maximized");
options.setExperimentalOption("prefs", chromePrefs);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);  
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
driver = new ChromeDriver(capabilities);
PRASAD
  • 1
  • 1
0

I did used simple TAB and ENTER keys to act upon Keep/Discard buttons on the window. I didn't find any better solution for this issue and it is working for me as workaround.

if (File_Extn.contentEquals("msg")) // put extns like msg, pdf etc.. 
                    {           
                        for (int j=1; j<=TabCount; j++) // manually count total no. of tabs and replace with TabCount to reach keep button
                        {   
                            sleep(1);
                            System.out.println("hit tab keys to reach keep/discard button");
                            robot.keyPress(KeyEvent.VK_TAB);
                        }
                        System.out.println("hit enter key to click on keep button");
                         robot.keyPress(KeyEvent.VK_ENTER);
                    }
PRASAD
  • 1
  • 1
-1

you can try to add the following argument

chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--safebrowsing-disable-download-protection')
driver = webdriver.Chrome(chrome_options=chromeOptions)
cieunteung
  • 1,725
  • 13
  • 16
  • I tried your suggestion as well @cieunteung but I'm still getting the same notification as you see in the image above. – robots.txt Feb 02 '19 at 15:19