Selenium not using Default Chrome Profile

5.9k views Asked by At

Selenium will not load my default Chrome Profile and I cannot figure out why. I have tried both Profile 1 and a Default profile with the same error (below). I have confirmed with Task Manager that all Chrome windows are shut down before running this code. Any thoughts?

from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
import os
os.system("taskkill /f /im geckodriver.exe /T")
os.system("taskkill /f /im chromedriver.exe /T")
os.system("taskkill /f /im IEDriverServer.exe /T")
os.system("taskkill /f /im chrome.exe /T")

driver2 = r"C:\Users\xxx\.wdm\drivers\chromedriver\87.0.4280.20\win32\chromedriver.exe"
options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\Users\xxx\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1")
driver = webdriver.Chrome(executable_path=driver2, chrome_options=options)
driver.get("https://www.google.co.in")

Traceback (most recent call last):
  File "C:\Users\xxx\OneDrive\Python\pyReportRun.py", line 16, in <module>
    driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
  File "C:\Python38\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 76, in __init__
    RemoteWebDriver.__init__(
  File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Could not remove old devtools port file. Perhaps the given user-data-dir at C:\Users\xxx\AppData\Local\Google\Chrome\User Data\Profile 1 is still attached to a running Chrome or Chromium process

enter image description here

enter image description here

enter image description here

[1]: https://i.stack.imgur.com/qf1Rr.png

enter image description here

enter image description here

enter image description here

2

There are 2 answers

22
undetected Selenium On

This error message...

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

...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session as the user-data-dir which you have passed an an argument is still attached to a running Chrome Browsing Context.


Details

Using Default Chrome Profile for Test Automation will be against all the best practices as the Default Chrome Profile may contain either/all of the following:

  • browser settings
  • Extensions
  • Bookmarks
  • Apps
  • Saved Passwords
  • Browsing History
  • etc

So the Default Chrome Profile may not be in compliance with you Test Specification and may occasionally raise exceptions while trying to load. Hence you should always use a customized Chrome Profile.

You can find a detailed discussion in How to open a Chrome Profile through --user-data-dir argument of Selenium

If your usecase still warrants to use the Default Chrome Profile you need to ensure that all the , or are stopped/killed and you can follow the below mentioned details.

Here you can find a detailed discussion on Selenium : How to stop geckodriver process impacting PC memory, without calling driver.quit()?


Location of Default Chrome Profile

As per the documentation in How to Find Your Chrome Profile Folder on Windows, Mac, and Linux the location for Chrome’s default profile folder differs depending on your platform. The locations are:

  • Windows 7, 8.1, and 10: C:\Users\<username>\AppData\Local\Google\Chrome\User Data\Default
  • Mac OS X El Capitan: Users/<username>/Library/Application Support/Google/Chrome/Default
  • Linux: /home/<username>/.config/google-chrome/default

You need to replace <username> with the name of your user folder. The default profile folder is simply named Default (or default in Linux). However, if you’ve created additional profiles, their folder names are not as obvious. The name you assigned to the profile when you created it displays on a name button on the right side of the title bar on the Chrome window. Unfortunately, the name Chrome uses on the associated profile folder is a generic, numbered name like Profile 3.

If you need to know any of the Chrome Profile's folder name, you simply need to access chrome://version in the address bar and press Enter.

Snapshot:

DefaultChromeProfile

The Profile Path shows the location of the current profile. For example, the location of my Default profile in my Windows 10 system is C:\Users\Soma Bhattacharjee\AppData\Local\Google\Chrome\User Data\Default. You can select the path and copy it and paste it into File Explorer in Windows, the Finder on OS X or into a file manager like Nautilus in Linux to access that folder.


Sample Code (Windows 10)

Finally, to access the Default Chrome Profile you can use the following Python based solution:

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

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\username\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")

You can find a detailed discussion in How to use Chrome Profile in Selenium Webdriver Python 3

17
PDHide On
chrom_options.add_argument("user-data-dir=C:\\Users\robert.car\\AppData\\Local\\Google\\Chrome\\User Data")

chrom_options.add_argument("profile-directory=Profile 1")

user-data-dir considers profile as default , and you don't have to specify that . If its something else specify it through profile-directory argument

Step to create a profile:

open : chrome://version in address bar

enter image description here

copy the user dir folder completely to eg c:\tmp\newdir

open the copied user data (newdir) and search for folder called Default . This is the profile folder.

rename the Default folder as "Profile 1"

Now to use this :

chrom_options.add_argument("user-data-dir=c:\\tmp\\newdir")

chrom_options.add_argument("profile-directory=Profile 1")