Search code examples
seleniumselenium-webdriverselenium-chromedriver

Selenium Chromedriver : How to open a specific Chrome profile


I want Selenium to open and control one of my existing Chrome profile named Selenium. I've tried different solutions but none of them work.

Profile name : Selenium

Profile directory : Profile 5

1- First I've tried :

options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=C:/Users/raphg/AppData/Local/Google/Chrome/User Data')
options.add_argument('profile-directory=Profile 5') 

This opens the right chrome profile but in a Chrome window that is not controlled by Selenium. Plus, my script crashes with the error : selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument

2- Then I've tried to clone the profile directory and to reference it like this :

options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=C:/Users/path_where_I_put_the_directory') 
options.add_argument('profile-directory=Profile 5')

This time, it gives me no crashes, but this opens a Chrome profile that is like a clone of my Selenium profile and it is not connected to my Google account unlike the real one.

With images, real profile VS the clone :


Solution

  • Solved!

    In short: you need to clone the entire 'User Data' folder, not just the profile you'll be using.

    About the problems I had:

    • 1 - the error user data directory is already in use speaks for itself, I couldn't use any profiles if I had already a profil in use. Here I was using 'Default' (for browsing purpose) so using any profile in User Data folder with Selenium would give this error.
    • 2 - My guess it that the reason Selenium webdriver opened like a clone of my profile was because it was missing some files from the 'User Data' folder.

    So to solve this:

    1- Go to C:\Users\name\AppData\Local\Google\Chrome and copy the 'User Data' folder

    2- Paste it to wherever you want (let's say to C:/Users/my_scripts. So now you have C:/Users/my_scripts/User Data)

    3- Use the profile you want with Selenium

    options = webdriver.ChromeOptions()
    options.add_argument('user-data-dir=C:/Users/my_scripts/User Data') 
    options.add_argument('profile-directory=Default')
    # Or
    options.add_argument('profile-directory=Profile 5')