How to use existing session with Seleniumbase? - python

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.

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

Can't access to correct chrome profile with selenium and that make the new one under the same directory

Updated(21Apr2021)
I found the problem. Thank to #simpleApp to point out the problem. I didn't notice the space character could make a problem.Now it's working well
option.add_argument("--user-data-dir=C:\\Users\\yen.ld\\AppData\\Local\\Google\\Chrome\\User Data\\")
option.add_argument("--profile-directory=Profile 1") #no space
Hello i'm using the newest selenium and python and chrome driver . And i'm facing an issue that. Even though i input correct directory but after run the code then it make other profile name under the same directory. Basically these 2 profiles will co-exist
This is my code and i tried some option but no solution till now..Is there any solution about it? Thank you guys so much
option = webdriver.ChromeOptions()
option.add_argument("--user-data-dir=C:\\Users\\NEO\\AppData\\Local\\Google\\Chrome\\User Data\\")
option.add_argument("--profile-directory= Profile 6")
driver = webdriver.Chrome(executable_path=r'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe', options=option)

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 Webdriver using default option never returns if an existing Chrome window is opened [duplicate]

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.

How to modify the ChromeDriver settings during the execution of the program?

I am now using Python + selenium + Chromedriver and I am trying to log in the my account automatically.
To make my program faster, here is my strategy:
Firstly, disable the picture-loading setting when initialling the driver.(see the code blow)
Since my log in process needs validation code, after fetching the page, I want to enable the picture-loading setting so that I can click the "refresh" button to gain validation code picture.
Here is my code for step 1:
ChromeOption = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images":2}
ChromeOption.add_experimental_option("prefs", prefs)
Driver = webdriver.Chrome(executable_path = './chromedriver', chrome_options = ChromeOption)
However, I don't know how to modify the driver settings. I search the document all day and found nothing.
Thank you all!
You cannot alter the chromedriver settings after it has been spun off because :
It would mean that the existing chromedriver process needs to be killed and re-invoked with the new arguments, which would now mean that your test would need to be started off again.This would essentially mean that your test has to start off again from the first.

Categories

Resources