Browser options and capabilities let you control how the browser behaves during automated tests. This helps make tests more reliable and flexible.
Browser options and capabilities in Selenium Python
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument('--headless') # Run browser in headless mode options.add_argument('--window-size=1920,1080') # Set window size driver = webdriver.Chrome(options=options)
Use Options() to set browser-specific options.
Use add_argument() to add command-line switches to the browser.
options = Options()
options.add_argument('--disable-notifications')
driver = webdriver.Chrome(options=options)options = Options()
options.add_argument('--start-maximized')
driver = webdriver.Chrome(options=options)capabilities = webdriver.DesiredCapabilities.CHROME.copy() capabilities['acceptInsecureCerts'] = True driver = webdriver.Chrome(desired_capabilities=capabilities)
This script opens the browser in headless mode with a window size of 800x600, navigates to example.com, prints the page title, and then closes the browser.
from selenium import webdriver from selenium.webdriver.chrome.options import Options import time options = Options() options.add_argument('--headless') options.add_argument('--window-size=800,600') # Create driver with options driver = webdriver.Chrome(options=options) # Open a website driver.get('https://example.com') # Print the page title print(driver.title) # Close the browser driver.quit()
Always quit the driver with driver.quit() to close the browser properly.
Headless mode is useful for running tests on servers without a display.
Use browser options to make tests faster and avoid unwanted pop-ups or dialogs.
Browser options let you customize how the browser runs during tests.
Use options to run headless, set window size, disable notifications, and more.
Capabilities can set browser behaviors like accepting insecure certificates.
Practice
Options in Selenium WebDriver for browsers?Solution
Step 1: Understand what Options configure
Options allow you to set how the browser behaves during tests, such as running headless or setting window size.Step 2: Differentiate Options from other tasks
Options do not handle locating elements, assertions, or reports; those are separate tasks.Final Answer:
To customize browser settings like headless mode or window size -> Option AQuick Check:
Options = Browser customization [OK]
- Confusing Options with element locators
- Thinking Options generate reports
- Mixing Options with assertion logic
Solution
Step 1: Recall correct method to add arguments
In Selenium Python,add_argumentis used to add command line switches like '--headless'.Step 2: Check each option's syntax
Only options = webdriver.ChromeOptions() options.add_argument('--headless') usesadd_argument('--headless')correctly; others use invalid or non-existent methods.Final Answer:
options = webdriver.ChromeOptions() options.add_argument('--headless') -> Option AQuick Check:
Use add_argument('--headless') for headless mode [OK]
- Using non-existent set_headless method
- Assigning headless attribute directly
- Using add_option instead of add_argument
from selenium import webdriver
options = webdriver.FirefoxOptions()
options.set_preference('dom.webnotifications.enabled', False)
driver = webdriver.Firefox(options=options)
driver.get('https://example.com')Solution
Step 1: Understand set_preference usage
The methodset_preferencesets Firefox preferences; here it disables web notifications.Step 2: Analyze the effect on browser behavior
Passing these options to Firefox disables notifications on pages, so no pop-ups appear.Final Answer:
Firefox will launch with notifications disabled on the page -> Option CQuick Check:
set_preference disables notifications [OK]
- Assuming notifications still appear
- Confusing set_preference with add_argument
- Thinking code runs headless by default
from selenium import webdriver
options = webdriver.ChromeOptions()
options.set_preference('profile.default_content_setting_values.notifications', 2)
driver = webdriver.Chrome(options=options)Solution
Step 1: Check ChromeOptions methods
ChromeOptions does not have a method calledset_preference; this is Firefox-specific.Step 2: Correct way to disable notifications in Chrome
Chrome usesadd_experimental_optionwith 'prefs' dictionary to disable notifications.Final Answer:
set_preference is not a method of ChromeOptions -> Option BQuick Check:
Use add_experimental_option for Chrome prefs [OK]
- Using Firefox methods on ChromeOptions
- Passing wrong values for notification settings
- Confusing options with capabilities
Solution
Step 1: Set headless mode correctly
Useadd_argument('--headless')to enable headless mode in ChromeOptions.Step 2: Disable notifications properly
Useadd_experimental_option('prefs', {'profile.default_content_setting_values.notifications': 2})to disable notifications.Step 3: Accept insecure certificates
Set capabilityacceptInsecureCertsto True usingset_capability.Final Answer:
options = webdriver.ChromeOptions() options.add_argument('--headless') options.add_experimental_option('prefs', {'profile.default_content_setting_values.notifications': 2}) options.set_capability('acceptInsecureCerts', True) driver = webdriver.Chrome(options=options) -> Option DQuick Check:
Combine add_argument, add_experimental_option, set_capability [OK]
- Missing '--' in headless argument
- Using set_preference on ChromeOptions
- Setting acceptInsecureCerts to False by mistake
Ready to prove it?
Pass the test and this pattern moves unknown
