0
0
Selenium-pythonHow-ToBeginner · 4 min read

How to Use Browser Profile in Selenium for Custom Testing

To use a browser profile in Selenium, create a profile object for the browser (like FirefoxProfile or Chrome options with a user data directory) and pass it when starting the browser driver. This lets you customize settings, extensions, or keep login sessions during tests.
📐

Syntax

For Firefox, create a FirefoxProfile object and pass it to FirefoxOptions. For Chrome, specify the user data directory in ChromeOptions. Then start the driver with these options.

  • Firefox: Use FirefoxProfile to load or create a profile.
  • Chrome: Use ChromeOptions with add_argument('--user-data-dir=path') to specify a profile folder.
python
from selenium import webdriver
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

# Firefox profile example
profile_path = '/path/to/firefox/profile'
profile = FirefoxProfile(profile_path)
options = FirefoxOptions()
options.profile = profile

driver = webdriver.Firefox(options=options)
driver.get('https://www.example.com')
driver.quit()
💻

Example

This example shows how to launch Chrome with a custom user profile folder to keep cookies and settings between sessions.

python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import time

profile_path = '/path/to/chrome/profile'
options = Options()
options.add_argument(f'--user-data-dir={profile_path}')

service = Service()
driver = webdriver.Chrome(service=service, options=options)
driver.get('https://www.google.com')

# Wait to see the browser
time.sleep(5)
driver.quit()
Output
Browser opens with the specified Chrome profile, preserving cookies and settings.
⚠️

Common Pitfalls

  • Using a profile path that does not exist or is locked by another browser instance causes errors.
  • Not closing the browser properly can corrupt the profile.
  • Mixing incompatible browser versions with profiles can cause failures.
  • For Chrome, specifying a profile folder that is used by a running Chrome instance will fail.

Always ensure the profile folder is closed before using it in Selenium.

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

# Wrong: Using profile folder while Chrome is open
options = Options()
options.add_argument('--user-data-dir=/path/to/used/profile')

try:
    driver = webdriver.Chrome(options=options)
except Exception as e:
    print('Error:', e)

# Right: Close Chrome before running Selenium with profile
# Then run the same code without error
Output
Error: Chrome is already running with this profile.
📊

Quick Reference

BrowserHow to Use ProfileNotes
FirefoxUse FirefoxProfile and pass to FirefoxOptionsProfile folder path must exist and not be locked
ChromeUse ChromeOptions with --user-data-dir argumentProfile folder must be closed before use
EdgeSimilar to Chrome, use user-data-dir in EdgeOptionsSame profile locking rules as Chrome

Key Takeaways

Create and pass a browser profile or user data directory to customize browser behavior in Selenium.
Always ensure the profile folder is not used by another browser instance to avoid errors.
Using profiles helps keep cookies, extensions, and settings between test runs.
Close browsers properly to prevent profile corruption.
Chrome and Edge share similar profile usage methods; Firefox uses a dedicated profile object.