0
0
Selenium Pythontesting~5 mins

Browser profile configuration in Selenium Python

Choose your learning style9 modes available
Introduction

Browser profile configuration lets you customize browser settings before tests run. This helps test real user scenarios like saved passwords or blocked pop-ups.

You want to test how your website behaves with cookies already saved.
You need to disable browser notifications during automated tests.
You want to set a specific language or timezone in the browser for testing.
You want to load browser extensions during testing.
You want to control browser privacy settings like blocking pop-ups.
Syntax
Selenium Python
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

profile = FirefoxProfile()
profile.set_preference('key', 'value')

options = Options()
driver = webdriver.Firefox(firefox_profile=profile, options=options)

Use set_preference to change browser settings before starting the browser.

FirefoxProfile is mainly for Firefox; other browsers have different ways to set profiles.

Examples
Sets the browser homepage to example.com before starting Firefox.
Selenium Python
profile = FirefoxProfile()
profile.set_preference('browser.startup.homepage', 'https://example.com')
Disables web notifications in the browser during tests.
Selenium Python
profile = FirefoxProfile()
profile.set_preference('dom.webnotifications.enabled', False)
Disables JavaScript in the browser to test site behavior without scripts.
Selenium Python
profile = FirefoxProfile()
profile.set_preference('javascript.enabled', False)
Sample Program

This script creates a Firefox profile that sets the homepage and disables notifications. It then opens Firefox with this profile, navigates to about:home, and prints the current URL.

Selenium Python
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

# Create a new Firefox profile
profile = FirefoxProfile()

# Set homepage to example.com
profile.set_preference('browser.startup.homepage', 'https://example.com')

# Disable web notifications
profile.set_preference('dom.webnotifications.enabled', False)

# Prepare Firefox options
options = Options()

# Start Firefox with the custom profile
with webdriver.Firefox(firefox_profile=profile, options=options) as driver:
    driver.get('about:home')
    print('Current URL:', driver.current_url)
OutputSuccess
Important Notes

Browser profiles help simulate real user settings during tests.

Always close the browser after tests to free resources.

For Chrome or other browsers, profile setup is different; check their docs.

Summary

Browser profile configuration customizes browser settings before tests.

Use set_preference to change settings like homepage or notifications.

This helps test how your site works with different user browser setups.