Randomly Launching a Web Browser with Python’s Webbrowser Module

The webbrowser module in Python provides a high‑level interface for opening URLs in a browser. While it defaults to the system’s preferred browser, you can register arbitrary executables and invoke them by name. This makes it possible to pick a browser at random from a predefined list.

Start by collecting absolute paths to browser executables. For Windows the typical locations might be:

BROWSER_PATHS = [
    r'C:\Program Files\Mozilla Firefox\firefox.exe',
    r'C:\Program Files\Google\Chrome\Application\chrome.exe',
    r'C:\Program Files (x86)\Internet Explorer\iexplore.exe',
    r'C:\Users\Administrator\AppData\Local\115Chrome\Application\115chrome.exe',
    r'D:\Program Files (x86)\SouExplorer\SogouExplorer\SogouExplorer.exe',
]

Next, define a function that randomly selects a path, registers it with a short name, opens a URL, waits a few seconds, and then forcefully kills the browser process. The registration step is handled by webbrowser.register(), where the first argument is a custom identifier, the second can be set to None, and the third is an instance of webbrowser.BackgroundBrowser wrapping the executable path.

To terminate the browser we use taskkill on Windows. The process name is extracted from the last element of the path. Using subprocess.run() avoids shell overhead and makes the command clearer.

import webbrowser
import random
import time
import subprocess
from pathlib import Path

def launch_random_browser(url, browser_paths):
    """Pick a random browser from the list, open the URL, then close it."""
    chosen = random.choice(browser_paths)

    if not Path(chosen).exists():
        # Fall back to default browser if path is invalid
        print(f"Path {chosen} not found, using default browser.")
        webbrowser.open_new_tab(url)
        return

    # Derive a short name and the process image name
    exe_name = Path(chosen).name               # e.g. 'firefox.exe'
    browser_id = Path(chosen).stem            # e.g. 'firefox'

    # Register the browser
    webbrowser.register(browser_id, None, webbrowser.BackgroundBrowser(chosen))
    webbrowser.get(browser_id).open_new_tab(url)
    print(f"Opened {url} with {browser_id}")

    # Give the browser time to load the page
    time.sleep(5)

    # Forcefully close the browser using its image name
    subprocess.run(['taskkill', '/f', '/im', exe_name], capture_output=True)
    print(f"Closed {exe_name}")

if __name__ == '__main__':
    target_url = 'https://www.example.com'
    launch_random_browser(target_url, BROWSER_PATHS)

If you want to avoid closing the browser automatically, simply omit the subprocess.run call. The webbrowser module also supports other backends such as GenericBrowser if you need to pass command‑line arguments. Note that the exact list of paths must reflect the actual installations on the machine; the ones above are only examples. For a cross‑platform approach, you would need different path lists per operating system or discover installed browsers through the registry on Windows or xdg-open on Linnux.

When you run the script, it selects a random executable, verifies its exitsence, opens the URL, and then terminates the browser after a short delay. This technique is useful for automated testing, scraping sessions that need to avoid fingerprinting, or simply demonstrating a feature across multiple browsers.

Tags: python webbrowser Random browser-automation Windows

Posted on Tue, 11 Aug 2026 16:59:40 +0000 by goldilok