How to control file upload with Selenium + Python bindings in Chrome

Where can I find documentation that describes the options that can be used with the Selenium and Chrome web browser? I want to open the link in a web browser (to get credentials), but not 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 a 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 a lot of discussion on this topic, but none of the solutions work. For example:

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

not working for me

The same goes 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'm not sure I understand what is wrong

The problem I am facing is that it starts to download the file every time I open a link with the name file (1) file (2) .... file (99), then, starting at 100, a popup opens Save As window, so I’d like to either not upload the file at all, or to be able to move it to a specific folder in the Trash.

How can I find which options can be used with add_argument and add_argument? I tried to take a look at Chrome: // about /, but did not see direct correspondence.

Many thanks.

Greetings.

Fabienne.

+6
file google-chrome download selenium-webdriver
source share
2 answers

The path you specified for the default directory is invalid. Either avoid backslashes, or print 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 settings:

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

+16
source share

The difference in the world is to use the slash "/" when specifying the directory in which you want to load things.

I assume this is because this directory will be exported to something like Powershell, where the usual backslash "\" will not work properly.

0
source share

All Articles