How to use Chrome Profile in Selenium Webdriver Python 3

140.5k views Asked by At

So whenever I try to use my Chrome settings (the settings I use in the default browser) by adding

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\Users\... (my webdriver path)")
driver = webdriver.Chrome(executable_path="myPath", options=options)

it shows me the error code

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes n 16-17: truncated \UXXXXXXXX escape

in my bash. I don't know what that means and I'd be happy for any kind of help I can get. Thanks in advance!

13

There are 13 answers

2
undetected Selenium On BEST ANSWER

As per your question and your code trials if you want to open a Chrome Browsing Session here are the following options:

  • To use the default Chrome Profile:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = webdriver.ChromeOptions()
    options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
    driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
    driver.get("https://www.google.co.in")
    
  • Note: Your default chrome profile would contain a lot of bookmarks, extensions, theme, cookies etc. Selenium may fail to load it. So as per the best practices create a new chrome profile for your @Test and store/save/configure within the profile the required data.

  • To use the customized Chrome Profile:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2")
    driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
    driver.get("https://www.google.co.in")
    
  • Here you will find a detailed discussion on How to open a Chrome Profile through Python

0
saitamatechno On

Printing docker's remote driver profile path and using it:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
options = Options()
#options.add_argument("user-data-dir=/tmp/.org.chromium.Chromium.jMPqvU/Default/") #Path to your chrome profile
driver = webdriver.Remote("http://192.168.1.236:4444/wd/hub", options=options)
def print_profile():
    driver.get("chrome://version")
    profile=driver.find_element(By.XPATH, "/html/body/div[1]/table/tbody/tr[8]/td[2]")
    print(profile.text)
print_profile()

#driver.get("https://www.tradingview.com/chart/45CxtJik/")
#print(driver.title)

Note: If the driver hangs there, it means the driver cannot find the path. For me, I wrote my path as
"/tmp/.org.chromium.Chromium.jMPqvU/Default"
And it hangs there, restart the docker container and try again with this path:
"/tmp/.org.chromium.Chromium.jMPqvU/Default/"

You need to add "/" symbol at the end.

2
uzumaki On

The accepted answer is wrong. This is the official and correct way to do it:

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

options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\path\to\chrome\user\data") #e.g. C:\Users\You\AppData\Local\Google\Chrome\User Data
options.add_argument(r'--profile-directory=YourProfileDir') #e.g. Profile 3
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")

To find the profile folder on Windows right-click the desktop shortcut of the Chrome profile you want to use and go to properties -> shortcut and you will find it in the "target" text box.

0
Lucas Tierney On

Make sure you've got the path to the profile right, and that you double escape backslashes in said path.

For example, typically the default profile on windows is located at:

"C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\Default"

0
Roochiedoor On

Are you sure you are meant to be putting in the webdriver path in the user-data-dir argument? That's usually where you put your chrome profile e.g. "C:\Users\yourusername\AppData\Local\Google\Chrome\User Data\Profile 1\". Also you will need to use either double backslashes or forward slashes in your directory path (both work). You can test if your path works by using the os library e.g.

import os
os.list("C:\\Users\\yourusername\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1")

will give you the directory listing.

I might also add that occasionally if you manage to crash chrome while running webdriver with a nominated user profile, that it seems to record the crash in the profile and the next time you open chrome, you get the Chrome prompt to restore pages after it exited abnormally. For me personally this had been a bit of headache to deal with and I no longer use a user profile with chromedriver because of it. I could not find a way around it. Other people have reported it here, but none of their solutions seemed to work for me, or were not suitable for my test cases. https://superuser.com/questions/237608/how-to-hide-chrome-warning-after-crash If you don't nominate a user profile it seems to create a new (blank) temporary one each time it runs

0
Fırat KAPAR On
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


# path of your chrome webdriver
dir_path = os.getcwd()
user_profile_path = os.environ[ 'USERPROFILE' ]

#if "frtkpr" which is ll be your custom profile does not exist it will be created.
option.add_argument( "user-data-dir=" + user_profile_path + "/AppData/Local/Google/Chrome/User Data/frtkpr" )

driver = webdriver.Chrome( dir_path + "/chromedriver.exe",chrome_options=option )
baseUrl = "https://www.facebook.com/"
driver.maximize_window()
driver.get( baseUrl )
0
DANLEP On

I managed to launch my chrome profile using these arguments:

ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data");
options.addArguments("--profile-directory=Profile 2");
WebDriver driver = new ChromeDriver(options);

You can find out more about the web driver here

0
AzyCrw4282 On

To get the path, follow the steps below.

In the search bar type the following and press enter

enter image description here

This will then show all the metadata. There find the path to the profile

enter image description here

0
therion On

You simply have to replace the '\' to '/' in your paths and that'll resolve it.

0
Denis Borisov On

This is how I managed to use EXISTING CHROME PROFILE in php selenium webdriver. Profile 6 is NOT my default profile. I dont know how to run default profile. It is IMPORTANT not to add -- before chrome option arguments! All other variants of options didnt work!

<?php
//...
$chromeOptions = new ChromeOptions();
$chromeOptions->addArguments([
    'user-data-dir=C:/Users/MyUser/AppData/Local/Google/Chrome/User Data',
    'profile-directory=Profile 6'
]);

$host = 'http://localhost:4444/wd/hub'; // this is the default
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $chromeOptions);
$driver = RemoteWebDriver::create($host, $capabilities, 100000, 100000);

To get name of your chrome profile, go to chrome://settings/manageProfile, click on profile icon, click "Show profile shortcut on my desktop". After that right click on desktop profile icon and go to properties, here you will see something like "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --profile-directory="Profile 6".

Also I recommend you to close all chrome instances before running this code. Also maybe you need to TURN OFF chrome settings > advanced > system > "Continue running background apps when Google Chrome is closed".

0
freedom1776 On
  1. Get profile name by navigating to chrome://version from your chrome browser (You'll see Profile Path, but you only want the profile name from it (e.g. Profile 1)
  2. Close out all Chrome sessions using the profile you want to use. (or else you will get the following error: InvalidArgumentException)
  3. Now make sure you have the code below (Make sure you replace UserFolder with the name of your userfolder.
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\EnterYourUserFolder\\AppData\\Local\\Google\\Chrome\\User Data") #leave out the profile
options.add_argument("profile-directory=Profile 1") #enter profile here
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe", chrome_options=options)
0
srinath reddy On

this worked for me 100% and it showed up my selected profile.

enter image description here

1
Ayush Gupta On

None of the given answers were working for me so I researched a bit and now the working code is for is this one. I copied the user dir folder from Profile Path from chrome://version/ and made another argument for the profile as shown below:

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

options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=C:\\Users\\gupta\\AppData\\Local\\Google\\Chrome\\User Data')
options.add_argument('profile-directory=Profile 1')
driver = webdriver.Chrome(executable_path=r'C:\Program Files (x86)\chromedriver.exe', options=options)
driver.get('https://google.com')