7

I am trying to get Selenium to open Chrome just as if I was opening it myself, i.e. I should be logged into my accounts like Facebook.

I have the following code:

def startChrome():
    options = webdriver.ChromeOptions() 
    options.add_argument("user-data-dir=/Users/alexiseggermont/Library/Application Support/Google/Chrome/Default/")
    driver = webdriver.Chrome(chrome_options=options)
    driver.set_page_load_timeout(60)
    return driver

driver = startChrome()
url = 'https://www.facebook.com'
driver.get(url)

However this gets me to Facebook without being logged in. I have checked chrome://version and the profile URL is in fact correct. What am I doing wrong?

Using Python 3, Chrome Version 63.0.3239.84, MacOS High Sierra

Alexis Eggermont
  • 7,665
  • 24
  • 60
  • 93

2 Answers2

9

Remove the Default/ from the end of your path

options.add_argument("user-data-dir=/Users/alexiseggermont/Library/Application Support/Google/Chrome/")

On a PC, it would typically be something like:

options.add_argument('user-data-dir=C:/Users/{USERNAME}/AppData/Local/Google/Chrome/User Data')

You'll need to check you've got compatible versions of chromedriver and chrome - the easiest way to do that, is to check that both are up to date.

If it crashes immediately on opening, check the chromedriver help page. Maybe try disabling all extensions, to see if that helps.

410 gone
  • 782
  • 14
  • 25
  • 2
    Thanks. That seems to open a new window with the right profile, but then it crashes (Message: unknown error: Chrome failed to start: exited abnormally (Driver info: chromedriver=2.34.522932 (4140ab217e1ca1bec0c4b4d1b148f3361eb3a03e),platform=Mac OS X 10.13.2 x86_64)) – Alexis Eggermont Jan 02 '18 at 14:32
  • Ah, I don't really know much about Macs. Is there anything here that helps: https://sites.google.com/a/chromium.org/chromedriver/help/chrome-doesn-t-start . And maybe disable all extensions, see if that helps. – 410 gone Jan 02 '18 at 14:53
  • Linux ubuntu: '/home//.config/google-chrome/Default' – Aqua 4 Apr 22 '19 at 18:11
  • For me, it works chrome_options.add_argument("user-data-dir=/Users/username/Library/Application Support/Google/Chrome/profileName") – ahsan Sep 09 '20 at 12:48
  • this also works for me, and i can start more than one webdriver with it, but it does give an error. – deleted Jun 07 '21 at 10:38
3

As an addition to EnergyNumbers' answer, you also have to make sure that you've actually quit chrome before launching your script.

In my case I constantly got the error Chrome failed to start: exited abnormally (unknown error: DevToolsActivePort file doesn't exist)

But that instantly disappeared when I quit chrome before launching the script. Apparantly, no 2 instances of the same profile can be opened at the same time.

Maurice
  • 4,829
  • 7
  • 41
  • 50