Multimedia and Browser Modules

webbrowser

High-level interface for asking an available browser to display a URL. Browser selection and success behavior depend on the host environment; opening a URL is not equivalent to fetching or validating it. This example mocks the browser controller and never launches a browser. The mock fixes the internal browser-selection list solely for deterministic testing; it is not a public configuration interface.

Formal Syntax & Structure

python syntaxpython
import webbrowser

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
import webbrowserfrom unittest.mock import patch, Mockcontroller = Mock()controller.open.return_value = Truewith patch.object(webbrowser, "get", return_value=controller), patch.object(webbrowser, "_tryorder", ["demo"]):    print(webbrowser.open("https://example.invalid"))
Python Reference