Disabling Browser Notifications

Introduction

Disabling Browser Notifications in Selenium allows you to prevent browser notification permission popups from appearing during automation testing. Many modern websites request permission to send notifications when they are opened. These popups can block page elements and interrupt automated test execution.

Selenium allows you to configure browser preferences so that notification permission requests are automatically blocked before the browser starts. This ensures that your automation scripts execute smoothly without requiring manual interaction.

Disabling browser notifications is a common browser configuration technique used in Selenium automation frameworks to improve test stability and reliability.

In this tutorial, you will learn what browser notifications are, why they should be disabled, their syntax, practical examples, real-world applications, common mistakes, best practices, and frequently asked interview questions.


What are Browser Notifications?

Browser notifications are permission requests displayed by websites asking users whether they want to receive notifications.

For example:

example.com wants to:

Show Notifications

[Allow]   [Block]

During automation testing, these popups may cover important page elements and cause Selenium scripts to fail.

By disabling browser notifications, Selenium automatically blocks these permission requests.


Why Do We Disable Browser Notifications?

Browser notifications are disabled to:

  • Prevent notification popups from interrupting automation.

  • Improve test reliability.

  • Avoid element interaction failures.

  • Execute unattended automation tests.

  • Create a consistent browser environment.

  • Speed up test execution.


Syntax

Create Chrome Options

options = webdriver.ChromeOptions()

Configure Notification Preferences

options.add_experimental_option(
    "prefs",
    {"profile.default_content_setting_values.notifications": 2},
)

Launch Chrome

driver = webdriver.Chrome(options=options)

Practical Example

The following example demonstrates how to disable browser notifications in Chrome.

The automation script creates Chrome Options, configures Chrome to automatically block notification permission requests, launches the browser, opens the Selenium practice website, and verifies that the page loads successfully.

from selenium import webdriver


# Topic: 8. Browser Configuration - Disabling Browser Notifications
# Practice site: https://the-internet.herokuapp.com/
#
# Browser notification prompts can be suppressed through Chrome preferences so
# they do not interrupt automated tests.


def test_disable_browser_notifications():
    options = webdriver.ChromeOptions()

    options.add_experimental_option(
        "prefs",
        {"profile.default_content_setting_values.notifications": 2},
    )

    driver = webdriver.Chrome(options=options)

    try:
        driver.get("https://the-internet.herokuapp.com/")

        assert driver.title == "The Internet"

    finally:
        driver.quit()

Output

Chrome notification preferences configured successfully.

Browser launched.

Notification permission requests are automatically blocked.

Website opened successfully.

Page Title:
The Internet

Assertion Passed.

Test Executed Successfully.

Understanding the Code

Import WebDriver

from selenium import webdriver

Imports the Selenium WebDriver module.


Create Chrome Options

options = webdriver.ChromeOptions()

Creates a Chrome Options object to configure browser settings.


Configure Notification Preferences

options.add_experimental_option(
    "prefs",
    {"profile.default_content_setting_values.notifications": 2},
)

This configures Chrome to automatically block notification permission requests.

The value:

2

means Block Notifications.

Common notification values:

ValueDescription
1Allow notifications
2Block notifications

Launch Chrome

driver = webdriver.Chrome(options=options)

Starts Chrome using the configured browser preferences.


Open the Website

driver.get("https://the-internet.herokuapp.com/")

Navigates to the Selenium practice website.


Verify the Page Title

assert driver.title == "The Internet"

Confirms that the website loaded successfully.


Close the Browser

driver.quit()

Closes all browser windows and ends the WebDriver session.


Browser Execution Flow

Create Chrome Options
          │
          ▼
Configure Notification Preferences
          │
          ▼
Launch Chrome
          │
          ▼
Browser Automatically Blocks Notifications
          │
          ▼
Open Website
          │
          ▼
No Notification Popup Appears
          │
          ▼
Assertions Pass
          │
          ▼
Close Browser

Notification Preference Values

Preference ValueBehavior
1Allow notifications automatically
2Block notifications automatically

Automation Testing Example

Suppose your automation tests an online shopping application.

When the homepage loads, Chrome displays:

shop.example.com wants to:

Show Notifications

[Allow]   [Block]

This popup hides the Login button and causes Selenium to throw an ElementClickInterceptedException.

By configuring notification preferences before launching Chrome, Selenium blocks the popup automatically, allowing the test to continue without interruption.


Real-World Example

Consider a CRM application that requests notification permissions when users log in.

A nightly regression suite containing hundreds of Selenium test cases runs on a CI/CD server.

By disabling browser notifications:

  • No notification popups appear.

  • Login automation executes without interruption.

  • Test failures caused by permission dialogs are eliminated.

  • Automation execution becomes faster and more reliable.


Common Mistakes Beginners Make

Forgetting to Pass Chrome Options

Creating Chrome Options alone is not enough.

Incorrect

options = webdriver.ChromeOptions()

options.add_experimental_option(...)

driver = webdriver.Chrome()

Correct

driver = webdriver.Chrome(options=options)

Configuring Preferences After Launching Chrome

Browser preferences must be configured before creating the WebDriver instance.


Using the Wrong Preference Value

1

allows notifications.

2

blocks notifications.

Always use the appropriate value based on your testing requirements.


Best Practices

  • Configure notification preferences before launching the browser.

  • Block notifications unless your test specifically verifies notification functionality.

  • Keep browser configuration centralized in your automation framework.

  • Use notification preferences consistently across test suites.

  • Verify browser behavior after browser updates.

  • Always close the browser using driver.quit().


Conclusion

Disabling Browser Notifications helps Selenium execute automated tests without interruption from notification permission popups. By configuring Chrome preferences before launching the browser, automation engineers can create a clean and stable testing environment.

This technique is widely used in Selenium frameworks because it improves test reliability, prevents popup-related failures, and enables smooth unattended automation execution.


Frequently Asked Questions (FAQs)

What are Browser Notifications?

Browser notifications are permission requests displayed by websites asking users whether they want to receive notifications.


How do I disable browser notifications in Chrome?

options = webdriver.ChromeOptions()

options.add_experimental_option(
    "prefs",
    {"profile.default_content_setting_values.notifications": 2},
)

What does the value 2 mean?

The value 2 tells Chrome to automatically block notification permission requests.


Why should browser notifications be disabled during automation?

Because notification popups can cover page elements, interrupt test execution, and cause Selenium scripts to fail.


When should notifications be enabled?

Enable notifications only when your test cases specifically verify browser notification functionality.


Key Takeaways

  • Browser notification popups can interfere with Selenium automation.

  • Configure notification preferences using add_experimental_option("prefs", ...).

  • Use profile.default_content_setting_values.notifications = 2 to block notifications.

  • Configure browser preferences before creating the WebDriver instance.

  • Disabling notifications improves automation stability and reliability.

  • Notification popups often cause element interaction failures if not handled properly.

  • Block notifications unless your test specifically requires them.

  • Disabling Browser Notifications is a commonly used Selenium browser configuration technique and an important interview topic.