Based on the posts here and here I am trying to use a chrome webdriver in selenium to be able to download a file. Here is the code so far
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_experimental_option("profile.default_content_settings.popups", 0)
chrome_options.add_experimental_option("download.prompt_for_download", "false")
chrome_options.add_experimental_option("download.default_directory", "/tmp")
driver = webdriver.Chrome(chrome_options=chrome_options)
But this alone results in the following error:
WebDriverException: Message: unknown error: cannot parse capability: chromeOptions
from unknown error: unrecognized chrome option: download.default_directory
(Driver info: chromedriver=2.24.417424 (c5c5ea873213ee72e3d0929b47482681555340c3),platform=Linux 4.10.0-37-generic x86_64)
So how to fix this? Do I have to use this 'capability' thing? If so, how exactly?
for chrome in mac os, the download.defaultdirectory did not work for me and fortunately savefile.default_directory works.
Some tips:
chromium and chromedriver should have same version.
Typically chromium package should have chromedriver inside, you can find it in the install dir. If you are using ubuntu/debian, execute
dpkg -L chromium-chromedriver
.Have a correct Chrome preference config.
as Satish said, use
options.add_experimental_option("prefs", ...)
to config selenium+chrome. But sometimes the config may change by time. The beset way to get newest and workable prefs is to check it in the chromium config dir. For example,~/.config/chromium/Default/Preferences
In my case, the code is:
I think that the easiest way to save arbitrary file (i.e. image) using WebDriver is to execute JavaScript which will save file. No configuration required at all!
I use this library FileSaver.js to save a file with desired name with ease.
From your exception, you are using
chromedriver=2.24.417424
.What are the versions of Selenium and Chrome browser that are you using?
I tried the following code with:
And it works:
Make sure you are using a browser that is supported by your chromedriver (from here, should be
Chrome v52-54
).Try this. Executed on windows
(How to control the download of files with Selenium Python bindings in Chrome)