Selenium Python Chromedriver Change File Download Path - python

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())

Related

Selenium chromdriver saving files with .crdownload extension

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.

Selenium + Firefox + Python: Download Directory issue

I have read these QA :
Downloading file to specified location with Selenium and python
Access to file download dialog in Firefox
and this blog : Working with file download in Python Selenium WebDriver
I then executed this code to specify the right path for the download:
from selenium import webdriver
my_path = r'C\some_dir\where_I\want_to\store\the_downloads'
profile = webdriver.FirefoxProfile()
profile.set_preference("brower.download.foldeList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", my_path)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv/pdf")
driver = webdriver.Firefox(executable_path=driver_path, firefox_profile=profile)
My issue is that the documents are not saved in my_path but in the default download directory of firefox.
EDIT 1:
I am unable to change the browser.download.folderList parameter.
Here is my command:
profile.set_preference('brower.download.folderList', '2')
I also tried each of these:
profile.set_preference('brower.download.folderList', 2)
profile.set_preference("brower.download.folderList", '2')
profile.set_preference("brower.download.folderList", 2)
When I open the about:config page on the opened Firefow window, brower.download.folderList is set to 1.
Sorry for the mistake.
I wrote brower instead of browser...

How to save a downloaded file to a specific relative directory with selenium in python

I am trying to download a file with Selenium and python into a specific relative directory but I can't.
I tried the following codes that I have seen in the web:
options = webdriver.ChromeOptions()
options.add_argument("download.default_directory=/folder")
driver = webdriver.Chrome(chrome_options=options)
and this:
import webdriver
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : '/folder'}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
but they do not work.
I also tried the following and it kind of works, but the select location window have to pop up, which I will have to manually click "Select"
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
prefs = {"profile.default_content_settings.popups": 0,
"download.default_directory":
r"C:\Users\user_dir\Desktop\\",#IMPORTANT - ENDING SLASH V IMPORTANT
"directory_upgrade": True}
options.add_experimental_option("prefs", prefs)
browser=webdriver.Chrome(<chromdriver.exe path>, options=options)
Do anyone know why and how to fix it? Or are there any alternative solutions that allow me to know the directory of the file and access the file it just downloaded?
I wouldn't recommend using the browser for downloading content, leave it to Chrome developers/testers, instead you should rather get href attribute of the <a> element or src attribute for other elements you want to download and obtain it using for example requests library
If the website you're getting information from requires authentication you can obtain cookies from the browser and pass them to requests.Session object.

How to change the target directory for a screenshot using Selenium webdriver in Firefox or Chrome

I want to make a screenshot of a webpage and save it in a custom location using Selenium webdriver with Python. I tried saving the screenshot to a custom location using both Firefox and Chrome but it always saves the screenshot in the project dir. Here is my Firefox version:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.dir",
'C:\\Users\\User\\WebstormProjects')
binary = FirefoxBinary("C:\\Program Files\\Mozilla Firefox\\firefox.exe")
def foxScreen():
driver = webdriver.Firefox(firefox_binary=binary,
firefox_profile=profile)
driver.get("http://google.com")
driver.save_screenshot("foxScreen.png")
driver.quit()
if __name__ == '__main__':
foxScreen()
And here is my Chrome version:
from selenium import webdriver
options = webdriver.ChromeOptions()
prefs = {"download.default_directory": r'C:\\Users\\User\\WebstormProjects',
"directory_upgrade": True}
options.add_experimental_option("prefs", prefs)
chromedriver =
"C:\\Users\\User\\Downloads\\chromedriver_win32\\chromedriver.exe"
def chromeScreen():
driver = webdriver.Chrome(chrome_options=options,
executable_path=chromedriver)
driver.get("http://google.com")
driver.save_screenshot("chromeScreen.png")
driver.quit()
if __name__ == '__main__':
chromeScreen()
I have tried different notations for the location I want the screenshot saved to but that does not seem to help. What should I change so it does not save the screenshot to the project directory but to a given custom location?
You need to consider a couple of facts as follows:
profile.set_preference('key', 'value')
set_preference(key, value) sets the preference that we want in the firefox_profile. This preference is in effect when a specific Firefox Profile is invoked.
save_screenshot(filename)
As per the documentation save_screenshot(filename) saves a screenshot of the current window to a PNG image file. This method returns False if there is any IOError, else returns True. Use full paths in your filename.
Args:
filename: The full path you wish to save your screenshot to. This should end with a .png extension.
Usage:
driver.save_screenshot(‘/Screenshots/foo.png’)
So, save_screenshot(filename) expects the full path you wish to save your screenshot to. As you were using:
driver.save_screenshot("foxScreen.png")
Hence the screenshot was always saved within the project directory.
Solution
To save the screenshot in a different directory you need to pass the absolute path as follows:
driver.save_screenshot("./my_directory/foo.png")
Reference
You can find a detailed discussion in How to take screenshot with Selenium WebDriver
Could try adding a few more options. This worked for me:
prefs = {"download.default_directory": r"\download\directory",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True}

How can I disable file download in Webdriver ChromeProfile

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)

Categories

Resources