Changing The Download Path After Opening Selenium Firefox Browser - python

I have a collection of files that i download with a loop. I want to folder these files separately. Before the browser opens, I can change the default download path with the options parameter. However, I want to folder the files I will download separately after the browser is opened.
Can you please help?
`
optns = Options()
optns.set_preference("browser.download.folderList", 2)
optns.set_preference("browser.download.manager.showWhenStarting", False)
optns.set_preference("browser.download.dir", "/Users/emrevolkanucar/Work/Ekap Py/Files")
optns.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
browser = webdriver.Firefox(options=optns)
???
optns.set_preference("browser.download.dir", "/Users/emrevolkanucar/Work/Ekap Py/Files")
`

You can not do that.
Once driver object instance is created by browser = webdriver.Firefox(options=optns) you can no more change it settings.
The only ways you can do here are:
re-define optns.set_preference("browser.download.dir", "/Users/emrevolkanucar/Work/Ekap Py/Files") and then create a new driver instance. I'm not sure you really want to do that.
After downloading files to defined (or default) downloading folder you can programmatically move those file to separate target folders. This approach seems better.

Related

How to load default profile with Selenium when Chrome is already open?

Following info on this answer (How to load default profile in Chrome using Python Selenium Webdriver?) I have this piece of code which works fine provided Chrome is not open, otherwise an error is given.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chromeDriverPath = 'C:\\Users\\xxxx\\AppData\\Local\\SeleniumBasic\\chromedriver.exe'
userdatadir = 'C:\\Users\\xxxx\\AppData\\Local\\Google\\Chrome\\User Data'
chromeOptions = webdriver.ChromeOptions()
tmp = f"--user-data-dir={userdatadir}"
chromeOptions.add_argument(tmp) #Path to your chrome profile
driver = webdriver.Chrome(chromeDriverPath, options=chromeOptions)
print(driver.current_url)
If Chrome is already open this is the error: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
A possible workaround is to (programmatically) copy the Chrome default directory into a new one. This way all settings and cookies are copied as well.
Problem, in my case, is that this directory is >2GB; I can't every time copy so much data... It has no sense.
So is it impossible or can anyone help in opening Chrome with default profile even if it is open?
The answer is you cannot open two session with 1 user-directory. Web-driver won't let you do this, It is like to rename a file in Windows when the file is open.
If you want to open chrome without user-dir, you can simply open with --incognito it will not store anything or use user-dir.
Otherwise you have to close the session with driver.quit() at the end of the test (teardown). So you can run the next test without problem.
Or you can use another user direcotry path, so the sessions will not interfers each other

How do I navigate to a site in Selenium with this certain path?

This is the line of code I initiate the browser with:
browser = webdriver.Chrome(executable_path='C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe')
I use this because I want to have my regular browser etc. Otherwise I would have used:
browser = webdriver.Chrome(ChromeDriverManager().install())
Now, my problem is that when I use the method "browser.get('link')", it does not redirect the page to the specified link. (Which did not happen with the second line of code specified here!) What do you suggest I do to solve this problem? As I said, I am only after retaining the cookies etc.
Through the key executable_path instead the absolute path of chrome.exe you need to pass the absolute path of ChromeDriver executable. So your effective line of code will be:
browser = webdriver.Chrome(executable_path='C:\\path\\to\\chromedriver\\chromedriver.exe')

Selenium Firefox profile preferences download dialogue box

I am trying to automatically log into several websites and download reports.
In my profile preferences, I have set the following:
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", folder1)
profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile","text/plain,text/x-csv,text/csv,application/vnd.ms-excel,application/csv,application/x-csv,text/csv,text/comma-separated-values,text/x-comma-separated-values,text/tab-separated-values,application/pdf,text/html")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain,text/x-csv,text/csv,application/vnd.ms-excel,application/csv,application/x-csv,text/csv,text/comma-separated-values,text/x-comma-separated-values,text/tab-separated-values,application/pdf,text/html")
For all the websites, except for 1, the files download without the dialog box showing.
However, one of them always shows the dialog box.
I am thinking it is due to the filename being called "Download.CSV", with the csv file extension in capitals...but I'm not convinced.
All the other files that successfully downloaded from the other websites were in csv file with a lower case csv file extension. This is the only difference I can think of.
Am I missing something?
For anyone who will come across this issue, I solved it.
Although the file extension was "CSV", the MIME type from the server was listed as "application/octet-stream".
Adding this to my Firefox profile preferences fixed the issue.

Update chrome driver default download location while driver is running

This is about the ChromeDriver for Selenium. So I know I can set the default directory by passing arguments to the Driver constructor like so:
chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : "/path/to/directory/"}
chromeOptions.add_experimental_option("prefs",prefs)
driver=Chrome(chrome_options=chromeOptions)
But I want to change that directory after a criteria has been met. Say after 15 files have been downloaded, I need to change my download location to "/path/to/new_directory/". Haven't really been able to find a simple solution to this.
Alternatively, I am also happy if I can simply rename files after my criteria has been met (the criteria is not as simple as every 15th file but you get the idea) but somehow that sounds like it would be trickier because Selenium has no access to the downloads once they start.

Define download directory for chromedriver selenium with python

Everything is in the title!
Is there a way to define the download directory for selenium-chromedriver used with python?
In spite of many research, I haven't found something conclusive...
As a newbie, I've seen many things about "the desired_capabilities" or "the options" for Chromedriver but nothing has resolved my problem... (and I still don't know if it will!)
To explain a little bit more my issue:
I have a lot of url to scan (200 000) and for each url a file to download.
I have to create a table with the url, the information i scrapped on it, AND the name of the file I've just downloaded for each webpage.
With the volume I have to treat, I've created threads that open multiple instance of chromedriver to speed up the treatment.
The problem is that every downloaded file arrives in the same default directory and I'm no more able to link a file to an url...
So, the idea is to create a download directory for every thread to manage them one by one.
If someone have the answer to my question in the title OR a workaround to identify the file downloaded and link it with the current url, I will be grateful!
For chromedriver1 create a new profile, and inside that profile set download.default_directory to the desired location, and set this profile for chrome using chrome.profile. The selenium-chromedriver package should have some methods for creating new profiles (at least it does with ruby), as they need some special handling.
Chromedriver2 doesn't support setting the profile. You can set preferences with it. If you want to set the download directory this is how you do it:
prefs: { download: { default_directory: "/tmp" } }
The ruby selenium-webdriver doesn't support this feature yet, the python variant might do however.
I have faced recently the same issue. Tried a lot of solutions found in the Internet, no one helped. So finally I came to this:
Launch chrome with empty user-data-dir (in /tmp folder) to let chrome initialize it
Quit chrome
Modify Default/Preferences in newly created user-data-dir, add those fields to the root object (just an example):
"download": {
"default_directory": "/tmp/tmpX7EADC.downloads",
"directory_upgrade": true
}
Launch chrome again with the same user-data-dir
Now it works just fine.
Another tip: If you don't know file name of file that is going to be downloaded, create snapshot (list of files) of downloads directory, then download the file and find its name by comparin snapshot and current list of files in the downloads directory.
Please try the below code....
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
String downloadFilepath = "/path/to/download";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
options.setExperimentalOptions("prefs", chromePrefs);
options.addArguments("--test-type");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);

Categories

Resources