0
0
Selenium Pythontesting~5 mins

Browser options and capabilities in Selenium Python

Choose your learning style9 modes available
Introduction

Browser options and capabilities let you control how the browser behaves during automated tests. This helps make tests more reliable and flexible.

When you want to run tests in headless mode (without opening the browser window).
When you need to disable browser notifications or pop-ups during tests.
When you want to set the browser window size or start maximized.
When you want to add custom browser preferences like disabling images to speed up tests.
When you want to specify browser capabilities like accepting insecure certificates.
Syntax
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.

Examples
This disables browser notifications during tests.
Selenium Python
options = Options()
options.add_argument('--disable-notifications')
driver = webdriver.Chrome(options=options)
This starts the browser window maximized.
Selenium Python
options = Options()
options.add_argument('--start-maximized')
driver = webdriver.Chrome(options=options)
This sets the browser to accept insecure SSL certificates.
Selenium Python
capabilities = webdriver.DesiredCapabilities.CHROME.copy()
capabilities['acceptInsecureCerts'] = True
driver = webdriver.Chrome(desired_capabilities=capabilities)
Sample Program

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.

Selenium Python
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()
OutputSuccess
Important Notes

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.

Summary

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.