Browser profile configuration lets you customize browser settings before tests run. This helps test real user scenarios like saved passwords or blocked pop-ups.
Browser profile configuration in 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.
profile = FirefoxProfile() profile.set_preference('browser.startup.homepage', 'https://example.com')
profile = FirefoxProfile() profile.set_preference('dom.webnotifications.enabled', False)
profile = FirefoxProfile() profile.set_preference('javascript.enabled', False)
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.
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)
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.
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.
Practice
Solution
Step 1: Understand browser profile role
A browser profile allows setting preferences like homepage, notifications, or extensions before tests run.Step 2: Identify the main purpose
This customization helps simulate different user environments during testing.Final Answer:
To customize browser settings before running tests -> Option BQuick Check:
Browser profile = Customize settings [OK]
- Thinking it speeds up test execution
- Confusing profile with test case writing
- Believing it changes test language
Solution
Step 1: Check method name and argument types
The correct method isset_preferencewith lowercase 'p' and boolean False (not string).Step 2: Validate option values
profile.set_preference('dom.webnotifications.enabled', False) uses correct method and boolean False, which disables notifications properly.Final Answer:
profile.set_preference('dom.webnotifications.enabled', False) -> Option AQuick Check:
Correct method + boolean False = profile.set_preference('dom.webnotifications.enabled', False) [OK]
- Using wrong method name like setPreference
- Passing boolean as string 'False'
- Using integer 0 instead of boolean False
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
profile = FirefoxProfile()
profile.set_preference('browser.startup.homepage', 'https://example.com')
profile.set_preference('dom.webnotifications.enabled', False)
profile.update_preferences()
print(profile.default_preferences['browser.startup.homepage'])
What will be printed when this code runs?Solution
Step 1: Understand FirefoxProfile attributes
Thedefault_preferencesattribute is internal and not updated byset_preference. It may not contain the key.Step 2: Predict print behavior
Accessingprofile.default_preferences['browser.startup.homepage']will raise a KeyError because this key is not in default_preferences.Final Answer:
KeyError -> Option CQuick Check:
Accessing missing key = KeyError [OK]
- Assuming set_preference updates default_preferences
- Expecting print to show new homepage URL
- Ignoring that KeyError occurs on missing dict key
profile = FirefoxProfile()
profile.set_preference('dom.webnotifications.enabled', 'false')
profile.update_preferences()
What is the likely cause of this problem?Solution
Step 1: Check preference value type
The value 'false' is a string, but the preference expects a boolean False to disable notifications.Step 2: Understand effect of wrong type
Passing string 'false' does not disable notifications, so they still appear.Final Answer:
The preference value should be boolean False, not string 'false' -> Option DQuick Check:
Boolean False needed, not string 'false' [OK]
- Using string 'false' instead of boolean False
- Thinking update_preferences() is deprecated
- Believing notifications can't be disabled
Solution
Step 1: Set preferences correctly
Useset_preferencewith correct keys and boolean False for notifications.Step 2: Update preferences and pass profile correctly
Callupdate_preferences()and pass profile asfirefox_profile=profileto WebDriver.Final Answer:
Code snippet in Option A correctly sets preferences, updates them, and launches Firefox with profile -> Option AQuick Check:
Set prefs + update + firefox_profile param = profile = FirefoxProfile() profile.set_preference('browser.startup.homepage', 'https://testsite.com') profile.set_preference('dom.webnotifications.enabled', False) profile.update_preferences() driver = webdriver.Firefox(firefox_profile=profile) [OK]
- Passing profile without keyword argument
- Using string 'False' instead of boolean False
- Forgetting update_preferences() call
Ready to prove it?
Pass the test and this pattern moves unknown
