0

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?

Andrew
  • 89
  • 1
  • 12
  • Can you not start your script by killing all existing instances of chrome and chromedriver? – JeffC Jan 13 '22 at 04:10

1 Answers1

2

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

  • Good points !!! +1 – undetected Selenium Jan 12 '22 at 13:00
  • ok, thanks; I'll make a reason for it. My goal is, when navigating on a page with login or 2-factor login, firing a python script that get specific data on the page with beautifulsoup (if page is static) or with selenium (if an interaction is needed in order to get the wanted datas). But from your and @undetectedSelenium's answer I understand there's unfortunately still no way for it – Andrew Jan 12 '22 at 17:23