2

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.

user279163
  • 33
  • 1
  • 7
  • I dont want to login with code, because it asks two factor authentication. – user279163 Aug 03 '22 at 18:34
  • Does this answer your question? [How do you use credentials saved by the browser in auto login script in python 2.7?](https://stackoverflow.com/questions/35641019/how-do-you-use-credentials-saved-by-the-browser-in-auto-login-script-in-python-2) – Chris Aug 03 '22 at 18:35

3 Answers3

3

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}")
Luke Hamilton
  • 637
  • 5
  • 19
0

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.

Lukas Tomek
  • 96
  • 12
0
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

Nishith Savla
  • 310
  • 2
  • 10