If you want to use custom chrome profile of user, and want to automate that browser will all login details, extensions, cookies already stories with all the profile data you want then for this case you can use the code below.

  • Set up ChromeOptions to customize the Chrome browser.
  • Specify the user data directory in ChromeOptions to use a custom Chrome profile.

How to create Chrome profile in Selenium WebDriver: Working Code

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time


# Using WebDriverManager to automatically download and manage ChromeDriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(r"user-data-dir=C:\\Users\\yawer\\AppData\\Local\\Chromium\\User Data\\Default")

driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(),chrome_options=chrome_options)
# URL to navigate to
url = r'https://www.google.com/search?q=cryptobool.com'
# Open the URL in the Chrome browser
driver.get(url)

time.sleep(4)

Selenium Chrome profile not working? : Fixing Path issues

Note: You just need to find your profile e.g C:\Users\yawer\AppData\Local\Google\Chrome\User Data\Default is default chrome user profile storage location, instead of single backslash you have to put double slash so that would change to C:\\Users\\yawer\\AppData\\Local\\Google\\Chrome\\User Data\\Default and just replace it in the code after user-data-dir=, and it will load that custom profile. (yawer is name of my username on computer, you might have your own computer username here )

Selenium webdriver chrome profile Python working code: explanation in detail (Each line)

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time

These lines import the necessary modules for our code. webdriver from the selenium package provides the interface for controlling web browsers, ChromeDriverManager from webdriver_manager.chrome helps manage and download the ChromeDriver, and time is a standard Python module for working with time-related operations.

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(r"user-data-dir=C:\\Users\\yawer\\AppData\\Local\\Chromium\\User Data\\Default")

Here, we create an instance of ChromeOptions, which allows us to customize the behavior of the Chrome browser controlled by Selenium. We then use add_argument() to add a command-line argument to specify the user data directory, which is the directory where Chrome stores user-specific data like profiles, cookies, and history.

driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), chrome_options=chrome_options)

This line creates an instance of the Chrome WebDriver using webdriver.Chrome(). We pass the executable_path parameter with the result of ChromeDriverManager().install() to automatically download and manage the appropriate version of ChromeDriver. We also provide the chrome_options object to set the desired options for the Chrome browser.

url = r'https://www.google.com/search?q=cryptobool.com'

Here, we define the url variable which contains the URL of the web page we want to navigate to. In this case, it is set to https://www.google.com/search?q=cryptobool.com, which is a Google search query for the term “cryptobool.com”.

driver.get(url)

This line instructs the WebDriver to open the specified URL (url) in the Chrome browser using the get() method. It loads the web page and waits for it to load completely.

time.sleep(4)

Here, we use the sleep() function from the time module to pause the program execution for 4 seconds. This adds a delay, allowing time for the web page to load or for other actions to be performed before moving on to the next line of code.

When should you load custom chrome profile in selenium python, chrome web driver ?

Consider a scenario where you need to be logged in to twitter for some purpose, since while testing your test case, you might have to run it tens to hundred times just for testing and developing it further, and just logging in and logging out will cause huge amount of time loss, so for this purpose it would make sense to load profile.
When not to use
If you want unique session, or anonymously want to surf sites without caring about data being stored then this would be the way to go.

If interested in checking ChromeOptions most used features and some of its popular usage i.e arguments then you can read more about ChromeOptions selenium class and its cheat sheet here.

Well this concludes loading profile path problem in selenium python and how you can put profile of your own choice after this tutorial, if you still have any questions or have stumbled upon strange errors then feel free to discuss with me in the comment box below, I will make sure to look at them with best of my efforts, hopefully 🙂 Good luck!

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *