Search code examples
python-3.xdebuggingselenium-webdriverpathselenium4

Why does opt.add_argument('--user-data-dir='+r'path') work but opt.add_argument('--user-data-dir='+fr'"{path}"') doesn't as an option to Selenium?


I have realized of something very weird when trying to deploy a chrome driver using --user-data-dir and --profile-directory from the user on Python 3.9.7, see below:

If you compile the following code:

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

opt = Options() #the variable that will store the selenium options

opt.add_argument('--user-data-dir='+r'C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data') #Add the user data path as an argument in selenium Options
opt.add_argument('--profile-directory=Default') #Add the profile directory as an argument in selenium Options
s = Service('C:/Users/ResetStoreX/AppData/Local/Programs/Python/Python39/Scripts/chromedriver.exe')

driver = webdriver.Chrome(service=s, options=opt) 
driver.get('https://opensea.io/login?referrer=%2Faccount')

You get successfully a chrome driver instance using the corresponding --user-data-dir and --profile-directory:

preview1

Now, after killing all chrome driver instances using the following code on cmd:

taskkill /F /IM chromedriver.exe

And then compiling this other code:

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

opt = Options() #the variable that will store the selenium options

path = input('Introduce YOUR profile path:')

opt.add_argument('--user-data-dir='+fr'"{path}"') #Add the user data path as an argument in selenium Options
opt.add_argument('--profile-directory=Default') #Add the profile directory as an argument in selenium Options
s = Service('C:/Users/ResetStoreX/AppData/Local/Programs/Python/Python39/Scripts/chromedriver.exe')

driver = webdriver.Chrome(service=s, options=opt) 
driver.get('https://opensea.io/login?referrer=%2Faccount')

For finally typing: C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data as input

You get this error:

WebDriverException: unknown error: Could not remove old devtools port file. Perhaps the given user-data-dir at "C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data" is still attached to a running Chrome or Chromium process

Why does that happen?

Isn't opt.add_argument('--user-data-dir='+fr'"{path}"') a valid way of passing this user data path:

path = C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data ?


Solution

  • I figured it out, I was creating a syntax error with opt.add_argument('--user-data-dir='+fr'"{path}"'), so I changed it for opt.add_argument('--user-data-dir='+fr'{path}'), the improved code would be the following:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    
    opt = Options() #the variable that will store the selenium options
    
    path = input('Introduce YOUR profile path:')
    
    opt.add_argument('--user-data-dir='+fr'{path}') #Add the user data path as an argument in selenium Options
    opt.add_argument('--profile-directory=Default') #Add the profile directory as an argument in selenium Options
    s = Service('C:/Users/ResetStoreX/AppData/Local/Programs/Python/Python39/Scripts/chromedriver.exe')
    
    driver = webdriver.Chrome(service=s, options=opt) 
    driver.get('https://opensea.io/login?referrer=%2Faccount')
    

    After compiling this code, the program will run without throwing any errors and get the same result as the first code shown in this post.