20

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.

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
Dr. Fabien Tarrade
  • 1,556
  • 6
  • 23
  • 49
  • Change download path to this - `"download.default_directory","C:\\Users\\xxx\\downloads\\Test"`. It works like charm for me. – Om Prakash Feb 09 '18 at 11:57

2 Answers2

41

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

Florent B.
  • 41,537
  • 7
  • 86
  • 101
  • Hi Florent, Thanks. You are right that in the example I gave you the path was wrong. With the right path it doesn't work and I think I found the issue. I realize that if I look at the setting of the Chrome web page I can see that I don't have right to change the download path (this is setup by my compagny!). Same things for **"download.prompt_for_download": False** with **True** or **False** I see the same things. It could be the same issue that I am not alllow to change this parameters. Thanks – Dr. Fabien Tarrade Nov 17 '16 at 19:49
  • You could try `options.add_argument('disable-component-cloud-policy')` to see if it disables the policy. – Florent B. Nov 18 '16 at 02:47
  • Hi Florent, I gave a try and it had no impact. I will try on another Laptop with these restriction to see if this works. I manage to solve most of the issue with .pdf by removing temp files in the download folder after the copy but the .jpg are display in the browser and some time crash the system. I try to play with .set_page_load_timeout(10) to avoid this issue. Thanks – Dr. Fabien Tarrade Nov 18 '16 at 12:57
  • 3
    If a PDF should be downloaded than this line is important in options: `"plugins.always_open_pdf_externally": True,` It is similar to checked menu Settings / Privacy - Content Settings / PDF Documents - Open PDF files in the default PDF viewer application (checkbox) – hynekcer Jan 12 '18 at 20:20
  • 2
    If a PDF should be downloaded than this line is important in options: `"plugins.always_open_pdf_externally": True,` It is similar to checked menu Settings / Privacy - Content Settings / PDF Documents - Open PDF files in the default PDF viewer application (checkbox) – hynekcer Jan 12 '18 at 20:21
  • use "savefile.default_directory": "/Users/creative/python-apps" in case of mac os catalina. – neelmeg Jan 24 '20 at 16:31
  • this worked for me. thanks. Now I'm looking for how to identify the filename used when saving, or set the filename when saved. – CodingMatters Jun 25 '20 at 09:58
-1

It makes all the difference in the world to use the forward slash "/" when specifying the directory in which you want things to be downloaded.

I'm guessing this is because that directory will be exported to something like the Powershell, where the usual backslash "\" won't properly work.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140