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

Time:08-04

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.

CodePudding user response:

You can use your profile that you use to open browser:

from selenium import webdriver
profile = webdriver.FirefoxProfile('/home/user/.mozilla/firefox/xxxx-release/') # this work for linux, if you use windows, you can find this file in local AppData
browser = webdriver.Firefox(executable_path='./geckodriver', firefox_profile =profile)

This will use your standard profile that you use with your standard browser, including logins, cookies etc.

Edit: I used firefox here, but the same principle works with chrome as well.

CodePudding user response:

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}")

CodePudding user response:

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

  • Related