I use Webdriver with Chromedriver on python.
I execute pages with automatic file downloading and I need to disable it.
What I need to set up in Chrome-driver download profile to disable automatic download?
I found next solution:
in CromeOption I created a folder that can't be created ("NUL"), so file can't be downloaded, but I can check everything I need on a page.
chrome_profile = webdriver.ChromeOptions()
profile = {"download.default_directory": "NUL", "download.prompt_for_download": False, }
chrome_profile.add_experimental_option("prefs", profile)
self.driver = webdriver.Chrome('chromedriver.exe',chrome_options=chrome_profile)
Related
Previously, I use to load and save cookies which is an inconvenient way for most websites. Instead, I use a User Profile to make it run flawlessly.
The problem is I able to load the User Profile but not save it. After I make some changes to the browser such as config addons, changing themes, logging into some websites, etc., exit Firefox via input('exit?') (see the code below), I reopen Firefox with the same User Profile and notice that nothing was saved to the User Profile (I use both methods, open Firefox and select the User Profile (via cmd "Firefox.exe -P"); rerun the script).
Selenium v3.14.0
Python v3.9.7
GeckoDriver v0.30.0 (64-bit)
Firefox v103.0.1 (64-bit)
import os
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
FireFoxPath = r"C:\\Program Files\\Mozilla Firefox\\firefox.exe"
DriverPath = r"BrowserDriver\geckodriver-v0.30.0-win64\geckodriver.exe"
FirefoxProfilePath = os.path.join(os.getcwd(), 'Playground_Temp')
# Initialize
options = webdriver.FirefoxOptions()
options.binary_location = FireFoxPath
options.set_preference('app.update.auto', False)
options.set_preference('app.update.enable', False)
capabilities = DesiredCapabilities.FIREFOX
capabilities["pageLoadStrategy"] = "none"
# Start Firefox
driver = webdriver.Firefox(
webdriver.FirefoxProfile(FirefoxProfilePath),
desired_capabilities=capabilities,
options=options,
executable_path=DriverPath
)
# Wait for user make changes
input('exit?')
# Quit Firefox
driver.quit()
I read a lot of answers about setting a download directory for Chrome, but couldn't find anything when you want to save a file to a specific directory, different from the default one, and you are using a specific profile.
The below code works fine, and when I download a file, it gets saved to the intended downloadFolderPath:
downloadFolderPath = 'P:\\reports\\attachments'
options = webdriver.ChromeOptions()
preferences = {"download.default_directory": downloadFolderPath,
"directory_upgrade": True,
"safebrowsing.enabled": True }
options.add_experimental_option("prefs", preferences)
options.headless = False
browser = webdriver.Chrome(executable_path='chromedriver.exe', options=options)
# browse to the page, click download, etc.
But when I try to do the same, this time using a specific Chrome profile, by adding user-data-dir and profile-directory arguments:
downloadFolderPath = 'P:\\reports\\attachments'
options = webdriver.ChromeOptions()
options.add_argument(r'--user-data-dir=C:\\Users\\my.name\\AppData\\Local\\Google\\Chrome\\User Data')
options.add_argument(r'--profile-directory=Profile 5')
preferences = {
"download.default_directory": downloadFolderPath,
"directory_upgrade": True,
"safebrowsing.enabled": True
}
options.add_experimental_option("prefs", preferences)
options.headless = False
browser = webdriver.Chrome(executable_path='chromedriver.exe', options=options)
# browse the page and download the file
Chrome indeed gets opened with the chosen profile, but the file that I download gets saved under the user profile default directory, C:\Users\my.name\Downloads instead of the one that I defined (P:\reports\attachments). It seems therefore that the preferences passed with options.add_experimental_option("prefs", preferences) are ignored when a profile different from the default one is used.
Or is there any different argument that needs to be used when working with specific profiles?
For info, I am on Windows 10, Python 3.9, using Chrome 95.0.4638.54 and chromedriver.exe is 95.0.4638.17
Thanks
I have written a code in python selenium that will login to Jira and then open another URL that is to download a report file.
Step 1- driver.get(jira.com) # I will login on this page
Step 2- driver.get('Another Jira URL.com\file.csv?somthing something....')# This URL will give me a csv file to download if I will put this in browser directly.
after step 2 It will open file dialogue it's downloading file in .temp format in download folder and after driver.quit() not closing file dialogue.
You need to set prompt_for_download is set to false, so that you would not see that download prompt at all. Create an options and pass them like below and then create object of chromedriver
Sample code :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
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(executable_path = driver_path, options = options)
Using the following selenium/chrome preferences, I can download a file to the same folder that I run the original python file from, and it works properly (file saves as download.xls)
DOWNLOAD_DIR = r'/mnt/ssd/rl-scrape/files/dev/dl/'
options = Options()
options.headless = True
options.add_argument("--incognito")
options.add_argument("--window-size=1920,1200")
options.add_argument("--disable-extensions")
options.add_experimental_option("prefs", {
"download.default.directory": DOWNLOAD_DIR,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True})
I understand that I've typo'd download.default_directory above. If I correct it, the file gets downloaded to the desired directory, but as download.xls.crdownload and not download.xls
Chrome adds the .crdownload extension while the download is in progress. Once the file is complete, it will rename the file to its proper name. As long as you see .crdownload, the download is not complete.
Add a time delay for the download process to finish before it quits the window. Worked for me.
I'm looking for a way to save different files to different locations in python using chromedriver. The code below sets chrome to download to folder_path without pop the download location dialogue first.
After clicking and downloading one file into folder_path (I skipped pasting this part of code cause I have no issue), I want to download another file into new_folder_path. But the code below gives me AttributeError: 'WebDriver' object has no attribute 'Chrome'. Any ideas if I can change download location for Chrome under the same webdriver?
folder_path = "C:\\Document"
def give_chrome_option(folder_path):
chromeOptions = webdriver.ChromeOptions() #setup chrome option
prefs = {"download.default_directory" : folder_path,
"download.prompt_for_download": False,
"download.directory_upgrade": True} #set path
chromeOptions.add_experimental_option("prefs", prefs) #set option
return chromeOptions
driver = webdriver.Chrome(chrome_options = give_chrome_option(folder_path)
driver.get(sample_url)
driver.Chrome(chrome_options = give_chrome_option(new_folder_path))
No, you have to re-instantiate WebDriver if you want to download to a different directory. Depending on what exactly do you need to do, a workaround described in the first answer here might be suitable for you (download to a temporary directory then move file using os.rename())