I know this question has been asked before...but I've tried multiple approaches and for some reason anything I download from the driver keeps going to my Downloads folder.
Basically I navigate to a website and download something by clicking a download link like so:
result.click()
This downloads the file fine. But I want to download it to a specific directory. I've tried doing these approaches to change the download directory:
driver = webdriver.Firefox()
profile = webdriver.FirefoxProfile()
driver.command_executor._commands["SET_CONTEXT"] = ("POST", "/session/$sessionId/moz/context")
driver.execute("SET_CONTEXT", {"context": "chrome"})
driver.execute_script("""
Services.prefs.setBoolPref('browser.download.useDownloadDir', true);
Services.prefs.setStringPref('browser.download.dir', arguments[0]);
""", directory)
driver.execute("SET_CONTEXT", {"context": "content"})
and
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", directory)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
Where directory is my desired location.
Neither of these worked...can anyone explain why or show me how to actually achieve this?
Thanks
As of 2021 the FirefoxProfile class used in earlier answers is deprecated, for the new selenium.webdriver.firefox.options.Options:
from selenium.webdriver.firefox.options import Options
options = Options()
options.set_preference("browser.download.folderList", 2)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir", "./downloads")
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
driver = webdriver.Firefox(options=options)
You're initializing your browser with the default profile since you're not passing any profile argument to your webdriver.Firefox()
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", directory)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
driver = webdriver.Firefox(profile)
below options, it's working fine for me.
# set download options
download_path = DOWNLOADS_PATH
# 0 means to download to the desktop, 1 means to download to the default "Downloads" directory, 2 means to use the directory
firefox_options.set_preference("browser.download.folderList", 2)
firefox_options.set_preference("browser.download.dir", download_path)
Related
I wrote the following procedure, based on Selenium and Chrome, to download a PDF file to a defined folder, after performing some actions on a web app:
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : path_to_destination,
"plugins.always_open_pdf_externally": True
# Additional options I've tried but didn't work
#,"download.prompt_for_download": False,
# 'profile.default_content_setting_values.automatic_downloads': 1,
# "helperApps.neverAsk.saveToDisk": mime_types,
# "plugin.disable_full_page_plugin_for_types": mime_types
}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(
executable_path=executable_path,
chrome_options=chrome_options
)
However, as soon as I click on the link that, normally, allows to visualize the pdf, the following unclickable page is displayed:
As soon as I manually click on it, everything works fine and the file is correctly downloaded to the indicated folder ("path_to_destination");
I tried with:
driver.find_element_by_xpath("//*[contains(#id, 'open-button')]").click()
# Or
driver.find_element_by_xpath("//*[contains(#id, 'main-content')]").click()
Since the xpath is:
//*[#id="main-content"]/a
But it does not work.
How can I either avoid opening this second page or clicking on the "Apri" (= Open) button?
P.S. Using Firefox and the following options, everything works fine:
# Setup
profile = webdriver.FirefoxProfile()
mime_types = "application/pdf,application/vnd.adobe.xfdf,"\
"application/vnd.fdf,application/vnd.adobe.xdp+xml"
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", full_destination)
# For PDFs
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", mime_types)
profile.set_preference("plugin.disable_full_page_plugin_for_types", mime_types)
profile.set_preference("pdfjs.disabled", True)
Try adding the following to prefs:
"download.prompt_for_download": False
Im trying to download some reports from web page, using selenium and python
when i click on download link firefox shows save/open dialog
I have used firefox profile configuration as follows
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile", "application/pdf,attachment/pdf")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf,attachment/pdf")
profile.set_preference("browser.download.dir", "c:\\firefox_downloads\\")
self.browser = webdriver.Firefox(profile)
but still it shows message box,
i have changed
self.browser = webdriver.Firefox(firefox_profile=profile)
like above but no chance >
Can any one help me to resolve this?
I have a little helper function which works for me.
It uses chromedriver though.
def driver_download(location_for_download):
# options = Options()
# options.headless = True
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory': location_for_download}
chrome_options.add_experimental_option('prefs', prefs)
# driver = webdriver.Chrome(chrome_options=chrome_options)
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver", chrome_options=chrome_options)
return driver
Note: For my particular application, I was unable to use the headless. But it should work if its a direct link to the file.
i use selenium firefox to download mp4 files with url(example:http://xxxx/xxxx.mp4), but always hang on after runing driver.get(file_url) , then timeout after 300sec(firefox default timeout 300 sec), the mp4 file has downloaded ,but driver.get(file_url) no response so i can't close firefox!
i have try file_url is normal web like http://www.google.com ,the response is ok!
can anybody help?
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.dir', path)
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'video/mp4')
driver = webdriver.Firefox(firefox_profile=profile)
driver.get(file_url)
You should not use get for this because you don't want a get to timeout. What you want is to create a link element at run-time using javascript and then click on it to perform the download
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.dir', "/tmp")
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'video/mp4')
profile.set_preference('media.mp4.enabled', False)
driver = webdriver.Firefox(firefox_profile=profile)
driver.get("http://www.google.com")
driver.execute_script("""
elem = document.createElement("a");
elem.id = "__download__";
elem.href = "http://techslides.com/demos/sample-videos/small.mp4";
elem.text = "__download__";
elem.textContent = "__download__content";
parentElem = document.getElementById('viewport');
parentElem.insertBefore(elem, parentElem.firstChild);
""")
driver.find_element_by_id("__download__").click()
An extra preference for disabling mp4 was also needed
profile.set_preference('media.mp4.enabled', False)
I've noticed this general format for auto saving in selenium in other posts
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", 'PATH TO DESKTOP')
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
driver = webdriver.Firefox(firefox_profile=profile)
But I'm downloading files from a source where I won't know what type will the files be. I've tried regex but it does not seem to work either
As to my knowledge of Selenium, there isn't a way to download all file types. You will have to list out all the required file types.
Here is a list of types: Link
Edit: Try the suggestions provided here: how can i download a file automatically on click of a link
Here I'm posting my suggestion for this issue, taking as an example the Brazilian Covid-19 files from the Ministry of Health:
from selenium import webdriver
from selenium.webdriver.common.by import By
firefox_options = webdriver.FirefoxOptions()
firefox_options.add_argument('--headless')
firefox_options.set_preference("browser.download.folderList", 2)
firefox_options.set_preference("browser.download.manager.showWhenStarting", False)
firefox_options.set_preference("browser.download.dir", 'your directory') #Do not forget to add your download path!
firefox_options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-7z-compressed;application/x-rar-compressed;application/zip")
driver = webdriver.Firefox(options=firefox_options)
driver.get("https://covid.saude.gov.br")
csv_button=driver.find_element(By.XPATH,"/html/body/app-root/ion-app/ion-router-outlet/app-home/ion-content/div[1]/div[2]/ion-button")
csv_button.click()
I hope this code might help someone with the same issue.
For me it was the combination of falsing the alwaysAsk and never ask to save and then specifying the file extension.
set_preference("browser.helperApps.alwaysAsk.force"), False
set_preference("browser.helperApps.neverAsk.saveToDisk", "application/zip;application/gz")
se = Service(gecko_path)
op = webdriver.FirefoxOptions()
op.add_argument('--headless')
op.set_preference("browser.download.folderList", 2)
op.set_preference("browser.download.manager.showWhenStarting", False)
op.set_preference("browser.download.dir", my_dir)
op.set_preference("browser.helperApps.alwaysAsk.force", False)
op.set_preference("browser.download.improvements_to_download_panel", True)
op.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/zip;application/gz")
driver = webdriver.Firefox(service=se, options=op)
basically what Abdinardo Oliveira said.
I want to be able to download a file from a website with a Python script by using Selenium. My problem is that creating and setting a Firefox Profile to the Firefox webdriver instance does not seem to work... Here is my code:
profile = webdriver.FirefoxProfile();
profile.set_preference("browser.download.folderList", 2);
profile.set_preference("browser.download.manager.showWhenStarting", False);
profile.set_preference("browser.download.dir", os.getcwd());
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/xml");
profile.update_preferences();
browser = webdriver.Firefox(firefox_profile=profile);
Even with the settings above, the open/save dialog box still opens up and the download does not happen. I then tried to do something simpler:
profile = webdriver.FireforProfile();
profile.set_preference("browser.startup.homepage", "http://www.google.com");
profile.update_preferences();
browser = webdriver.Firefox(firefox_profile=profile);
And all i get is firefox to open, that's it. This brings me to believe that the profile is not being set to the driver. Any help would be appreciated!
For anyone who comes along this here is a function that will return a webdriver with some common options. It shouldn't have a hardcoded path to the Firefox profile but I haven't gotten around to that yet
def get_firefox_driver(download_dir=None, use_profile=False, headless=False):
if download_dir is None:
download_dir = os.getcwd()
if use_profile:
try:
fp = webdriver.FirefoxProfile(r'C:\Users\MDDT0040\AppData\Roaming\Mozilla\Firefox\Profiles\u5jvcbqp.dev-edition-default-1525796820341',)
except:
print('cant get profile')
fp = webdriver.FirefoxProfile()
else:
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", int(2))
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.manager.showAlertOnComplete", False)
fp.set_preference("browser.download.dir", download_dir)
fp.set_preference('browser.helperApps.neverAsk.saveToDisk',
"text/csv,text/comma-separated-values,text/comma-separated-values;charset=UTF-8")
if headless:
options = Options()
options.headless = True
return webdriver.Firefox(firefox_profile=fp, firefox_options=options)
else:
return webdriver.Firefox(firefox_profile=fp)