0
0
Selenium Pythontesting~15 mins

Firefox configuration in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Configure Firefox browser to disable notifications and set custom download directory
Preconditions (2)
Step 1: Create a Firefox profile
Step 2: Set the profile preference to disable browser notifications
Step 3: Set the profile preference to automatically download files to a custom directory without prompt
Step 4: Launch Firefox browser with the configured profile
Step 5: Navigate to a test page that triggers a notification or file download
Step 6: Verify that notifications are disabled and files download automatically to the specified directory
✅ Expected Result: Firefox browser launches with notifications disabled and files download automatically to the custom directory without any prompt
Automation Requirements - Selenium with Python
Assertions Needed:
Verify Firefox browser is launched with the custom profile
Verify that browser notifications are disabled (no notification popup appears)
Verify that files download automatically to the specified directory
Best Practices:
Use FirefoxProfile to set preferences
Use explicit waits if needed
Use clear and maintainable locators
Handle browser setup and teardown properly
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import os
import time

def test_firefox_configuration():
    # Set custom download directory
    download_dir = os.path.join(os.getcwd(), "downloads")
    if not os.path.exists(download_dir):
        os.makedirs(download_dir)

    # Create Firefox profile and set preferences
    profile = webdriver.FirefoxProfile()
    # Disable notifications
    profile.set_preference("dom.webnotifications.enabled", False)
    # Set download directory
    profile.set_preference("browser.download.folderList", 2)  # 2 means use custom location
    profile.set_preference("browser.download.dir", download_dir)
    # Disable download prompt for common file types
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf,application/octet-stream")
    profile.set_preference("pdfjs.disabled", True)  # Disable built-in PDF viewer

    # Initialize Firefox options
    options = Options()
    options.profile = profile

    # Launch Firefox with profile
    driver = webdriver.Firefox(options=options)

    try:
        # Navigate to a test page that triggers download
        driver.get("https://file-examples.com/index.php/sample-documents-download/sample-pdf-download/")

        # Click the download button for a sample PDF file
        download_button = driver.find_element("xpath", "//a[contains(text(),'Download sample pdf file')]" )
        download_button.click()

        # Wait for download to complete (simple wait for demo purposes)
        time.sleep(5)

        # Verify file downloaded
        files = os.listdir(download_dir)
        assert any(file.endswith(".pdf") for file in files), "PDF file was not downloaded"

        # Since notifications are disabled, no popup should appear (cannot assert popup absence easily)
        # We rely on preference set and no exceptions thrown

    finally:
        driver.quit()

if __name__ == "__main__":
    test_firefox_configuration()

This script creates a Firefox profile with preferences to disable notifications and set a custom download directory. It disables the notification popups by setting dom.webnotifications.enabled to False. It sets the download folder to a local downloads directory and disables the download prompt for PDF files by setting browser.helperApps.neverAsk.saveToDisk. The built-in PDF viewer is disabled to force download.

The test navigates to a sample PDF download page and clicks the download link. It waits briefly to allow the file to download, then checks the download folder for a PDF file. The test asserts that the file exists, confirming the download worked automatically.

Finally, the browser is closed properly in a finally block to ensure cleanup.

Common Mistakes - 4 Pitfalls
{'mistake': 'Not setting the download directory preference correctly', 'why_bad': 'The browser will prompt for download location every time, breaking automation flow', 'correct_approach': "Set 'browser.download.folderList' to 2 and 'browser.download.dir' to the desired path"}
{'mistake': 'Not disabling notifications properly', 'why_bad': 'Notification popups can block test execution and cause flaky tests', 'correct_approach': "Set 'dom.webnotifications.enabled' preference to False in Firefox profile"}
Using hardcoded sleep instead of explicit waits
Not cleaning up downloaded files after test
Bonus Challenge

Now add data-driven testing with 3 different file types to download (PDF, CSV, XLS)

Show Hint