1

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

RCarmody
  • 712
  • 1
  • 12
  • 29

2 Answers2

2
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")
PDHide
  • 18,113
  • 2
  • 31
  • 46
  • When adding this, I still don't see my profile come up. The browser is still not signed in, and chrome//version shows this: C:\Users\ROBERT~1.CAR\AppData\Local\Temp\scoped_dir45504_1195749914\Profile 1 – RCarmody Dec 01 '20 at 14:04
  • What did you add , add th script you used – PDHide Dec 01 '20 at 14:40
  • You should add user dir as C:\Users\ROBERT~1.CAR\AppData\Local\Temp\scoped_dir45504_1195749914 and profile dir as Profile 1 – PDHide Dec 01 '20 at 14:41
  • Sorry that would have been helpful... I tried: options.add_argument("profile-directory=Profile 1") and options.add_argument("profile-directory=C:\\Users\robert.car\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1") – RCarmody Dec 01 '20 at 14:44
  • Updated the answer use both the user dir and profile for as showm – PDHide Dec 01 '20 at 14:47
  • I am now getting the same error as the original post – RCarmody Dec 01 '20 at 16:09
  • Ok sorry you should close all chrome before running the script – PDHide Dec 01 '20 at 16:11
  • It's being used by chrome you are already using so close all browser instant , entire chrome process and run script – PDHide Dec 01 '20 at 16:11
  • I did this, also checking Task Manager to confirm anything under Google Chrome was ended, with no luck. Thoughts? – RCarmody Dec 01 '20 at 18:00
  • Can you try copying the profile into a new folder and try ? – PDHide Dec 01 '20 at 18:47
  • Still showing the same error even when I copied the entire "Profile 1" folder as "Profile 1 - Copy" – RCarmody Dec 01 '20 at 21:10
  • how did you create the profile 1 and profile 1 folder ? – PDHide Dec 02 '20 at 05:48
  • Added steps on how to create a chrome profile correctly – PDHide Dec 02 '20 at 05:53
  • Just posted my code, screenshots of the user profile im using, the chrome version, my code/error, and my file explorer – RCarmody Dec 02 '20 at 13:42
  • @RCarmody the code shows your old code . Did you try what was suggested ? User die= blabla/user Data And profile = Profile 1 – PDHide Dec 02 '20 at 14:11
  • I thought so, but maybe not accurately.... When you say "copy the user dir folder completely to eg c:\tmp\newdir", do you mean copy the entire User Data folder and reference that instead? What's c:\tmp\newdir – RCarmody Dec 02 '20 at 14:35
  • Say you create a new folder inside c:\tmp folder and name it as newdir, copy the content of user data folder to this directory . And give userr-dir=C:\\tmp\\newdir and profile-dir=Profile 1 – PDHide Dec 02 '20 at 14:41
0

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I am not trying to reference my Default Profile, but rather Profile 1, given by C:\Users\robert.car\AppData\Local\Google\Chrome\User Data\Profile 1. We are getting the error whenever we add the profile like so: options.add_argument("profile-directory=Profile 1") – RCarmody Dec 01 '20 at 22:10
  • @RCarmody Keep it short sweet and simple. Extract the `Profile Path` from `chrome://version` and pass through `user-data-dir` argument. – undetected Selenium Dec 01 '20 at 22:15
  • That's what I had originally.... Getting rid of the Profile Directory step, I have just the folloowing: options.add_argument("user-data-dir=C:\\Users\robert.car\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1") and get the same error – RCarmody Dec 01 '20 at 22:19
  • @RCarmody Are you able to access _Profile 1_ manually, i.e. using _Chrome Profile 1_ and accessing `chrome://version`, the **Profile Path** shows _C:\Users\robert.car\AppData\Local\Google\Chrome\User Data\Profile 1_? – undetected Selenium Dec 01 '20 at 22:28
  • Yes, this is correct. I copied this path directly from chrome://version and then just adjusted the \ to \\ – RCarmody Dec 01 '20 at 22:30
  • @RCarmody That essentially means apparently you are closing the manually opened browser but a few of the subprocesses are still running/hanging around as a zombie. Hence you see the error. Try the script to wipe off all the _Chrome_ related processes. – undetected Selenium Dec 01 '20 at 22:38
  • Where was that script posted? Sorry if I missed it in all of this – RCarmody Dec 01 '20 at 23:00
  • @RCarmody Please have a look at the reference discussion linked within the section starting with the text _If your usecase still warrants to use the Default Chrome Profile you need to ensure that..._ – undetected Selenium Dec 01 '20 at 23:05
  • Looks like I'm still getting the same error.... I've updated the original code to what I currently have to avoid any confusion. Notice I've included the Profile 1 as well as the os.system taskkills – RCarmody Dec 01 '20 at 23:13
  • @RCarmody Your error comes out of a dangling `chrome.exe` process. You need to add `os.system("taskkill /f /im chrome.exe /T")` as well. – undetected Selenium Dec 01 '20 at 23:17
  • No luck there.... although it did kill something. Original code updated – RCarmody Dec 01 '20 at 23:51
  • Just posted my code, screenshots of the user profile im using, the chrome version, my code/error, and my file explorer – RCarmody Dec 02 '20 at 13:43
  • @RCarmody I've a question, how did the Chrome Profile _Profile 1_ get created? Generally, when you keep using the _Default Chrome Profile_ it would be only the `/User Data/Default` folder. Once you manually [create a new Chrome Profile](https://stackoverflow.com/questions/49270109/how-to-open-a-chrome-profile-through-python/49280195#49280195), _Profile 1_ and _Profile 2_ both gets created with a shortcut on the desktop. – undetected Selenium Dec 02 '20 at 14:03
  • Added screenshots of step by step how I added the Profile, found it on my desktop, copied the path, and updated my code but still no luck :( – RCarmody Dec 02 '20 at 14:31
  • @RCarmody If you are willing to use the _Default Chrome Profile_, use `user-data-dir` argument with `\\User Data\\Default`. If you are willing to use the newly created profile use `user-data-dir` argument with `\\User Data\\Profile 2`. – undetected Selenium Dec 02 '20 at 15:18
  • Not sure what you are suggesting.... Is the \\User Data\\Profile 2 something different than what I already have in my code? – RCarmody Dec 02 '20 at 15:59
  • Of coarse you are using `Profile 1`where the Default data would be dumped when you create a new _Chrome Profile_ – undetected Selenium Dec 02 '20 at 16:03
  • I'm not sure I'm following. If I need to use Profile 1 (rather than default), would this not be the correct line of code? options.add_argument("user-data-dir=C:\\Users\xxx\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1"). This is the same code that I've been trying – RCarmody Dec 02 '20 at 16:08
  • You aren't telling us how you got **Profile 1** directory as it isn't created unless you create a second _Chrome Profile_ manually. Once you create the second _Chrome Profile_ manually, **Profile 1** directory gets created. – undetected Selenium Dec 02 '20 at 16:12
  • Profile 1 & 2 have both been added through the "Add person" screenshot in my post. This then created a shortcut on my desktop as well as the folder within User Data. Does that make sense? – RCarmody Dec 02 '20 at 16:19
  • @RCarmody Now this information makes sense. Uninstall/reinstall Google Chrome and try to use `\\User Data\\Default` instead of `\\User Data\\Profile 1` – undetected Selenium Dec 02 '20 at 16:23
  • Uninstalled via "Add or Remove Programs" and then immediately went to Firefox, downloaded Chrome (64bit), ran the install exe, and then replaced \\User Data\\Profile 1 with \\User Data\\Default. No luck here – RCarmody Dec 02 '20 at 16:29