Test Overview
This test opens a Firefox browser with a custom profile that disables images to speed up loading. It verifies the browser starts with the profile applied by checking the page title.
This test opens a Firefox browser with a custom profile that disables images to speed up loading. It verifies the browser starts with the profile applied by checking the page title.
from selenium import webdriver from selenium.webdriver.firefox.options import Options from selenium.webdriver.common.by import By import unittest class TestFirefoxProfile(unittest.TestCase): def setUp(self): profile = webdriver.FirefoxProfile() # Disable images to speed up loading profile.set_preference('permissions.default.image', 2) profile.update_preferences() options = Options() self.driver = webdriver.Firefox(firefox_profile=profile, options=options) def test_page_title(self): self.driver.get('https://example.com') title = self.driver.title self.assertEqual(title, 'Example Domain') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Create FirefoxProfile and set preference to disable images | FirefoxProfile object configured with images disabled | — | PASS |
| 2 | Launch Firefox browser with the custom profile | Firefox browser opens with images disabled | — | PASS |
| 3 | Navigate to https://example.com | Browser loads example.com page without images | — | PASS |
| 4 | Get page title and assert it equals 'Example Domain' | Page title is 'Example Domain' | assertEqual(title, 'Example Domain') | PASS |
| 5 | Close the browser | Browser closed | — | PASS |
set_preference with lowercase 'p' and boolean False (not string).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?default_preferences attribute is internal and not updated by set_preference. It may not contain the key.profile.default_preferences['browser.startup.homepage'] will raise a KeyError because this key is not in default_preferences.profile = FirefoxProfile()
profile.set_preference('dom.webnotifications.enabled', 'false')
profile.update_preferences()
What is the likely cause of this problem?set_preference with correct keys and boolean False for notifications.update_preferences() and pass profile as firefox_profile=profile to WebDriver.Pass the test and this pattern moves unknown