Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control the download of files with Selenium + Python bindings in Chrome

Where can I find the documentation that describes the options I can use with Selenium and Chrome web browser? I want to open a link in a web browser (to get credential) but not to download the corresponding file (.pdf or .tiff or .jpeg). I am using Python 2.7, selenium 3.0.1 and Chrome version 54.0.2840.99 (and chromedriver.exe) on Windows 7 Laptop.

# Chrome web browser.
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')  
#options.add_argument('--disable-download-notification') #doesn't seems to work 
#options.add_experimental_option("prefs", {"download.default_directory","C:\Users\xxx\downloads\Test"}) # doesn't work
#options.add_experimental_option("prefs", {"download.prompt_for_download": False}) # doesn't seems to work
#options.add_experimental_option("prefs", {'profile.default_content_settings': {'images': 2}})# this will disable image loading in the browser
options.add_argument("user-agent="+user_agent_profile)
driver_main = webdriver.Chrome(chrome_options=options)

# Opening the web application portail.
driver_main.get("https://my_link")

I found many discussions on this topic but none of the solution works. For example:

add_experimental_option("prefs", {"download.default_directory","C:\Users\xxx\downloads\Test"})

doesn't work for me.

Same for:

add_experimental_option("prefs", {"download.prompt_for_download": False})

(I also try with "false").

While:

add_argument("user-agent="+user_agent_profile)

Seems to work!

I am not sure to understand what is wrong

The issue I got is that, it starts to download the file each time I open a link with name file(1) file(2) .... file(99) then starting at 100 it opens a popup window "Save As". So I would like to either don't download the file at all or be able to move it in a specific folder in the "Recycle Bin".

How do I find which options could be I used with add_argument and add_argument? I tried to look at Chrome://about/ but I couldn't see a direct correspondence.

Thanks a lot.

Cheers.

Fabien.

like image 324
Dr. Fabien Tarrade Avatar asked Nov 17 '16 12:11

Dr. Fabien Tarrade


People also ask

Can Selenium download files?

If you are an automation tester fixated on Selenium automation testing, the chances are that you might run into a requirement of testing a feature around downloading files. While being a powerful tool for performing automated browser testing, Selenium natively doesn't support download functionality.


1 Answers

The path you declared for the default directory is invalid. Either escape the back slashes or provide a literal string.

options = webdriver.ChromeOptions()
options.add_experimental_option("prefs", {
  "download.default_directory": r"C:\Users\xxx\downloads\Test",
  "download.prompt_for_download": False,
  "download.directory_upgrade": True,
  "safebrowsing.enabled": True
})
driver = webdriver.Chrome(chrome_options=options)

Here are the available preferences:

https://cs.chromium.org/chromium/src/chrome/common/pref_names.cc

like image 192
Florent B. Avatar answered Sep 20 '22 04:09

Florent B.