In Python, the webbrowser module provides a high-level interface for displaying web-based documents in the user's default browser.
In this chapter, we will learn the webbrowser module, its syntax, parameters, examples, built-in functions, and members.
The webbrowser module provides a high-level interface for opening web documents in the user's default web browser. We can use it to open URLs, launch new browser tabs or windows, and access specific browsers installed on the system. It can also be used as a command-line (CLI) tool. It accepts a URL as an argument along with some optional parameters.
It has the following syntax:
For executable code:
webbrowser.open(url, new=0, autoraise=True)
OR
For command-line interface (CLI):
python -m webbrowser [-n | -t] URL
The following parameters are used with the webbrowser module:
Here, we are going to take several examples that demonstrate the working of the webbrowser module.
The following example demonstrates how to use the webbrowser module to open a specific URL in a new web browser window.
Python
import webbrowser
webbrowser.open('https://www.tpointtech.com/chainmap-in-python')
print('Browser opened!')
Output:

Browser opened!
In the given example, we imported the webbrowser module. After that, we called the open() function from the module and passed the URL to open it in a new browser window. Finally, we printed a message after successfully opening the browser.
The following example demonstrates how to use the webbrowser module from the CLI with the -t parameter to open a URL in a new browser tab.
Output:

In the given example, we used the webbrowser module with the -t parameter followed by a URL in the command-line interface (CLI) of a code editor or terminal. When we run the command, the URL opens in a new tab of the default web browser.
The following example demonstrates how to use the webbrowser module from the CLI with the -n parameter to open a URL in a new browser window, if possible.
Python
python -m webbrowser -n "https://www.tpointtech.com/python-statistics-correlation-method"
Output:

In the given example, we used the webbrowser module with the -n parameter followed by a URL in the command-line interface (CLI) of a code editor or terminal. When we run the command, the URL opens in a new browser window, if possible.
The following table lists some important functions that are provided by the webbrowser module to open URLs in the web browser.
| Function | Descriptions |
|---|---|
| open() | It opens a specified URL in the system's default web browser. |
| open_new() | It opens a specified URL in a new browser window, if possible. |
| open_new_tab() | It opens a specified URL in a new tab of the default web browser. |
| get() | It is used to obtain a browser controller object for a specific browser type. After that, it can be used to open URLs using that browser. |
| register() | It is used to register a browser type so that it can be accessed and controlled through the webbrowser module. |
The following table shows some important members of the webbrowser module to open URLs in the web browser.
| Member | Descriptions |
|---|---|
| Chrome | Google Chrome can be specified as a browser type when using functions such as get() or register() to open URLs through the Chrome browser. |
| BackgroundBrowser | The BackgroundBrowser is a browser controller class in Python's webbrowser module that is used to launch a browser in the background without requiring it to become the active window. |
| BaseBrowser | The BaseBrowser is a base class in Python's webbrowser module that provides the basic structure for browser controller classes. Other browser controller classes can inherit from it to implement browser-specific behavior. |
| Opera | Opera is a browser controller in Python's webbrowser module that can be used to open URLs in the Opera browser. |
| UnixBrowser | The UnixBrowser is a browser controller class in Python's webbrowser module that is used to open URLs in web browsers on Unix-based operating systems. |
| Error | An error in Python's webbrowser module occurs when the requested browser cannot be found, opened, or used to perform the specified operation. |
| MacOSX | The MacOSX is a browser controller class in Python's webbrowser module used to open URLs in web browsaers on macOS. |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India