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.
Related
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.
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
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')
I would like seleniumbase to use my existing session when it launches vs starting a clean one.
How do I start selenium using an existing session?
webdriver:
driver = webdriver.Chrome('/snap/bin/chromium.chromedriver', options=option)
UPDATE:
This is marked as a duplicate which it is not.
Dear #TodorMinakov this code here didn't meet my expectations. I wanted whenever I run the python program to select the unique opened window and use selenium over that window. I will appreciate your help. – Youssof H
Same issue here. The referred URL does not grab an existing session.
You can use default chrome profile.
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=PATH") #)PATH is path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)
To find path to your chrome profile data you need to type chrome://version/ into address bar.
For ex. mine is displayed as C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default,
If path will not work, try to remove \Default\ so we end up with only C:\Users\pc\AppData\Local\Google\Chrome\User Data.
Better to copy-paste default profile outside, because there no way to use same session (cookies) in two browsers in same time.
This question already has answers here:
Selenium: Point towards default Chrome session
(2 answers)
Closed 3 years ago.
I have Google Chrome already opened where I am doing my own work. I am trying to run a Selenium Chrome driver using the default configuration stored in my C:\\Users\\Himanshu Poddar\\AppData\\Local\\Google\\Chrome\\User Data ddirectory.
But when I am launching Chrome using this default profile, my function never returns which I think is due to my another Chrome window opened on which I am working. Because I can see an alert appearing on my working Chrome window which says "chrome is controlled by test software".
Here is what I have tried
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:\\Users\\Himanshu Poddar\\AppData\\Local\\Google\\Chrome\\User Data")
chrome_path = r"C:\Users\Himanshu Poddar\Desktop\chromedriver.exe"
# This never returns
wd = webdriver.Chrome(chrome_path, chrome_options=options)
The last executing statement webdriver.Chrome never return and prints this to the console
[5972:6048:0614/210846.434:ERROR:cache_util_win.cc(21)] Unable to move the cache: 0
[5972:6048:0614/210846.435:ERROR:cache_util.cc(141)] Unable to move cache folder C:\Users\Himanshu Poddar\AppData\Local\Google\Chrome\User Data\ShaderCache\GPUCache to C:\Users\Himanshu Poddar\AppData\Local\Google\Chrome\User Data\ShaderCache\old_GPUCache_000
[5972:6048:0614/210846.435:ERROR:disk_cache.cc(185)] Unable to create cache
[5972:6048:0614/210846.435:ERROR:shader_disk_cache.cc(623)] Shader Cache Creation failed: -2
Opening in existing browser session.
What was expected:
A different instance of Chrome to be running with the same default profile that is independent of my current chrome window on which I am working.
Note that I tried the suggestions that I was getting for this question and none of them worked for me.
Edit
This issue is somewhat similar to mine but does not contains the solution to this problem.
Thanks to #pcalkins I was able to find an answer for the question. The very first step was to clone your User profile or whatever was there in C:\\Users\\Himanshu Poddar\\AppData\\Local\\Google\\Chrome\\User Data and rename it with some name. In my case I just renamed it with User data - Copy. Next step involves deleting the files that our current user who is using google chrome (in other window) from our cloned copy. Finally run the code as
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
chrome_path = r"C:\Users\Himanshu Poddar\Desktop\chromedriver.exe"
options = Options()
# new clone copy of user data supplied
options.add_argument("user-data-dir=C:\\Users\\Himanshu Poddar\\AppData\\Local\\Google\\Chrome\\User Data - copy")
wd = webdriver.Chrome(chrome_path, chrome_options=options)
Note that the update to my driver wasn't required in this case but its always a good practice to work with latest stable release of the software who knows what dependency problem you may run into.
Though I have cloned the profile and then supplied it to my function. Any answer that does not require copy to be generated and solves it just by including, tweaking, twisting any parameter will be accepted.