Stay logged in with Selenium using Chrome default profile - python

I want to use selenium with my Google profile, I created this code:
options = webdriver.ChromeOptions()
options.add_argument(r"user-data-dir=C:\Users\myprofile\AppData\Local\Google\Chrome\User Data") #Path to my chrome profile
driver = webdriver.Chrome(executable_path=r"C:\chromedriver.exe", options=options)
driver.get(url)
I got this error:
InvalidArgumentException: 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
I also tried another profile, but return the same error:
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=C:/Users/mxs/AppData/Local/Google/Chrome/User Data')
options.add_argument('--profile-directory=Profile 2')
driver = webdriver.Chrome(executable_path=r"C:\chromedriver.exe", options=options)
driver.get(url)
also, I tried codes in both Jupyter Notebook and VSCode

Suggestion 1
If you are okay with only one selenium process running at a time, you can use queue to achieve it. As long as you use only one process to access user-dir, it'll not cause any error.
Suggestion 2
If you are trying to stay logged in for some website, you can reuse cookies. In this case you don't need to use user-data-dir thing. Selenium will automatically create temporary user-data-dir on every run.
Suggestion 3
If you still want to use the same profile for every session, here is the hack to do it. You keep original --user-data-dir unused, instead you copy all the content of your original profile directory to some temporary directory and use it with --user-data-dir every time you initiate chrome selenium.

Suggestion
When you are trying to run your python script close all the chrome windows. I got the error
selenium.common.exceptions.InvalidArgumentException: 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
when I had one of the chrome windows open. So it is better to close all the chrome windows and try again.

Related

how can I test a remember me feature using selenium python

how can I write a test case validation for a remember me button as selenium every time opens a new instance of the browser with no cookies.
I heard ChromeOptions class can store cookies in a private file.
I tried searching for the argument and found-
"user-data-dir=/path/"
how can I use this path back into my next test case in order to validate it?
Add this code in your second loop
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\YOUR_USERNAME_HERE\\AppData\\Local\\Google\\Chrome\\User Data\\")
You may or may not then also need to re-instantiate the driver
driver = webdriver.Chrome(PATH, options=options)
and do the GET request
driver.get("http://127.0.0.1:8000/route_that_requires_remember_me")

What is the correct directory for user-data-dir in Selenium chrome.options

I'm trying to do some chrome automation using selenium on python, and I want to have my login info saved for youtube, so that I don't have to automate the login process. I read that the way to have chrome read my saved info is by using the user-data-dir option, but I can't get it to work.
I have looked at some questions here, such as this, and they seem to indicate that the correct usage of the argument user-data-dir in selenium for chrome is the profile path inside: C:\Users\adassa\AppData\Local\Google\Chrome\User Data. Such as Default or Profile 1. I can't seem to get the intended result using this kind of path. The only path that works for me is the User Data folder itself.
So when I do:
chrome_options.add_argument("--user-data-dir=" + r'C:\\Users\\adassa\AppData\\Local\\Google\\Chrome\\User Data')
The chrome instance is opened with the preferences set for the default profile. But that only works correctly when I close all other instances of chrome, otherwise it raises the error: 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
Which I believe is caused by my manual chrome instance blocking access to the info on the folder because I'm using it. I read that I can create a new chrome profile, so that it doesn't conflict with the one I normally use, but if I create a new profile manually on chrome, and use the profile path, as in:
chrome_options.add_argument("--user-data-dir=" + r'C:\\Users\\adassa\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1')
the new instance doesn't have the info I saved to the profile. That happens even if I use as path the User Data\Default folder. I have also tried different combinations using the option profile-dir, with no success.
After reading the docs, I tried using a folder that does not exist. In that case selenium does create the new folder, but when I try to login to youtube trough that selenium created instance, in order to save the info for the browser to use on the next time, I get this message (translation mine):
It was not possible to login. This browser or app may not be safe.
I would love to find a way to have my data saved so that I don't have to login everytime, and also not have to close all other chrome instances when running my script. Any help is appreciated.
chrome_options.add_argument("--user-data-dir=C:\\Users\\adassa\AppData\\Local\\Google\\Chrome\\User Data")
chrome_options.add_argument("--profile-directory=Profile 1")
as the name says usr-dir is for user-data folder and profile-directory is to specify the profile.
if profile folder is default , you don't have to specify that chrome takes it by default else specify it "--profile-directory=Profile 1"

How do you keep logged in with selenium driver?

I've been trying some things with selenium. However, I was annoyed that I could keep logged in google so, after some research, I found this script (from 2017) that is supposed to keep you logged in. I've tried it, and it works partially. It opens a new driver with my account logged in but then gives me this error:
selenium.common.exceptions.InvalidArgumentException: 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
I-ve tried to remove the path, to change it to C:\\Users\\pablo\\AppData\\Local\\Google\\Chrome\\User Data but nothing seems to work. It eithers opens a driver without my data or one with my data but then gives me an error.
Thank you in advance for your help.
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:\\Users\\pablo\\AppData\\Local\\Google\\Chrome\\User Data")
driver = webdriver.Chrome(chrome_options=options, executable_path="C:/Users/pablo/OneDrive/Bureau/Projets Python/Infos Insta/chromedriver")
driver.get('https://web.whatsapp.com/')
driver.quit()

How to run Explorer independently with Python Selenium

I'm trying to use IE using the selenium module in python.
I want to log in with different login IDs in multiple IE windows, but the login information in IE windows is shared with each other, so independent login is not possible.
I am using selenium version is 3.6 and the explorer version is 11.
How can I fix it?
For example, when I log in to Google email in the first explorer window, when the second explorer window is turned on, I am already logged in to the Google email.
I want to log in to the same site with a different ID at the same time.
IE_1 = ID_1
IE_2 = ID_2
IE_3 = ID_3
....
You can achieve separate logins by running the browser in "private" mode.
This is not specific to IE. You can do this in Chrome Firefox, and other browsers as well.
1. IE in private mode
From the docs, you can set "IE Command-Line Options".
Example below is directly from the documentation.
I have NOT tested the below code myself
as I don't have IE in my environment.
from selenium import webdriver
options = webdriver.IeOptions()
options.add_argument('-private')
options.force_create_process_api = True
driver = webdriver.Ie(options=options) # MIGHT WANT TO CHANGE
# Navigate to url
driver.get("http://www.google.com")
driver.quit()
If you don't have the $PATH set for IE, try this:
driver = webdriver.ie(executable_path="<YOUR_IE_PATH>",
options=options)
2. Chrome in private mode
For Chrome, I can confirm it is valid working code. It's a simplified version of what I use myself.
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(
executable_path="<YOUR_CHROME_DRIVER_PATH>",
chrome_options=chrome_options)
driver.get('https://google.com')
For the full list of acceptable "chrome_options", you can check out the reference. This link may not look "official", but it's actually referenced from the Chromedriver website (under "Recognized Capabilities" - "ChromeOptions object" - "args"), so we can safely rely on this doc.

selenium chrome attach default profile

i am using python ana selenium, to automate some process, but couldnt attached selenium to default chrome profile
i tried with,
capability = webdriver.DesiredCapabilities.CHROME
self.driver = webdriver.Remote('http://127.0.0.1:9515/wd/hib',capability)
of course, i started, chromedriver first, and also tried with,
import time
from selenium import webdriver
import selenium.webdriver.chrome.service as service
service = service.Service('./chromedriver')
service.start()
capabilities = {'chrome.binary': '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'}
driver = webdriver.Remote(service.service_url, capabilities)
driver.get('http://www.google.com/xhtml');
time.sleep(5) # Let the user actually see something!
driver.quit()
this causes, selenium.common.exceptions.WebDriverException: Message: u'Could not find Chrome binary at:
and also tried with,
self.driver = webdriver.Chrome("./chromedriver")
this works, but not default profile, and also wonder to know, how to open new window or new tab with this ?
thanks.
Don't just copy/paste something straight off the website! Have a look into that folder yourself, does it have anything in it?! My guess is no. This is why when you leave that bit off, it works fine, because it's looking for Chrome where it should exist!
Any way, more to the point you are using it wrongly!
If you want to give Selenium a different profile to use for Chrome, then you need to use the options class:
https://code.google.com/p/selenium/source/browse/py/selenium/webdriver/chrome/options.py
You want the add_argument function.
Why?
This is because to give Chrome another profile to use, you need to launch Chrome with a specific command line (specifically --user-data-dir):
http://www.chromium.org/user-experience/user-data-directory
The add_argument function exposes the ability to add command line switches.
So if you use the add_argument function, Selenium will simply pass whatever you give it, down to Chrome as being part of it's command line switches.
To find out where your chrome profile is located start chrome and type
chrome://version
in the address bar. Under "Profile Path:" you'll see the location of the profile you're currently using. For example:
~:\Users\<username>\AppData\Local\Google\Chrome\User Data\Default

Categories