Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a already logged-in chrome browser in selenium python

I want to open a browser that is logged in to my gmail account like my default browser in selenium. Is there a way to do this?

edit:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options) 

this method does not work for me.

like image 510
user279163 Avatar asked Jul 19 '26 20:07

user279163


2 Answers

Chrome Options

Chrome options are very particular on how you call them. Syntax needs to be followed with perfection. Correct your below syntax errors by changing your old code:

options.add_argument("user-data-dir=C:\\Path")

To the following code:

userdatadir = 'C:/Users/<user>/AppData/Local/Google/Chrome/User Data'
chromeOptions.add_argument(f"--user-data-dir={userdatadir}")
like image 159
Luke Hamilton Avatar answered Jul 22 '26 11:07

Luke Hamilton


options = webdriver.ChromeOptions()
options.add_argument(f"user-data-dir={CHROME_USER_DIR}")
options.add_argument("profile-directory=Default")

This will open the Default Profile in Chrome.

The user-data-dir looks something like C:\Users\<username>\AppData\Local\Google\Chrome\User Data

To open a different profile, replace the Default with your profile directory

like image 27
Nishith Savla Avatar answered Jul 22 '26 11:07

Nishith Savla