We use Chrome configuration to control how the Chrome browser behaves during automated tests. This helps tests run smoothly and match real user actions.
0
0
Chrome configuration in Selenium Python
Introduction
When you want to open Chrome in a special mode like incognito or headless.
When you need to set browser size or disable pop-ups during tests.
When you want to add extensions or change browser preferences for testing.
When you want to speed up tests by disabling images or animations.
When you want to run tests without opening the browser window (headless mode).
Syntax
Selenium Python
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options options = Options() # Add your desired options here options.add_argument('--headless') # Example: run Chrome without UI service = Service('path/to/chromedriver') driver = webdriver.Chrome(service=service, options=options)
Use Options() to set Chrome-specific settings before starting the browser.
Always provide the path to chromedriver that matches your Chrome version.
Examples
This opens Chrome in incognito mode, which does not save browsing history.
Selenium Python
options = Options()
options.add_argument('--incognito')
driver = webdriver.Chrome(options=options)This runs Chrome without opening a window and sets the browser size to 1920x1080 pixels.
Selenium Python
options = Options() options.add_argument('--headless') options.add_argument('--window-size=1920,1080') driver = webdriver.Chrome(options=options)
This disables browser notifications during tests to avoid pop-ups.
Selenium Python
options = Options() options.add_experimental_option('prefs', {'profile.default_content_setting_values.notifications': 2}) driver = webdriver.Chrome(options=options)
Sample Program
This test opens Chrome in headless mode with a set window size, navigates to example.com, prints the page title, then closes the browser.
Selenium Python
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options options = Options() options.add_argument('--headless') options.add_argument('--window-size=1200,800') service = Service('chromedriver') driver = webdriver.Chrome(service=service, options=options) driver.get('https://www.example.com') print(driver.title) driver.quit()
OutputSuccess
Important Notes
Headless mode is useful for running tests on servers without a display.
Always quit the driver to close the browser and free resources.
Use add_argument for command-line switches and add_experimental_option for preferences.
Summary
Chrome configuration lets you customize browser behavior during tests.
Use Options() to add arguments like headless or incognito modes.
Setting preferences helps avoid interruptions like pop-ups or notifications.