FactorPad
Build a Better Process

Syntax of the Python webbrowser Module with Examples

A relatively simple module allows you to programmatically open a web browser of your choice through a Python script.
  1. About - Review the purpose, rules and location of webbrowser.
  2. Assignment - Construct a webbrowser object with examples.
  3. Functions - Learn webbrowser function syntax with examples.
  4. Methods - See webbrowser syntax with parameter examples.
  5. Help - Find additional information locally.
face pic by Paul Alan Davis, CFA
Updated: February 24, 2021
For basic cases, this module may offer a simpler solution than Selenium WebDriver. See how easy it is.

Outline Back Tip Next

/ factorpad.com / tech / python / reference / python-webbrowser.html


An ad-free and cookie-free website.


A Guide to Working with Web Browsers in Python

Beginner

Python Reference

This Python reference offers programmers a quick way to learn Python and also serves as a source for reminders.

While the version documented here is Python 3.5.3, most of this is suitable for other versions of Python 3. Check your version for details.

Outline

  1. About the Python webbrowser Data Type
    1. How to access the webbrowser data type
  2. Python webbrowser Assignment
    1. Syntax
    2. Examples
  3. Python webbrowser Functions
    1. Syntax
    2. Examples
  4. Python webbrowser Methods
    1. Syntax
    2. Examples
  5. Find Local Help on Python webbrowser

1. About the Python webbrowser Data Type

The webbrowser data type can be used to select which browser to use to open a specified URL. Here we call that object a Browser Controller.

Otherwise, the webbrowser module's main purpose is to provide functions to open a URL, either by launching a new browser window, or by opening a new tab in the currently open browser.

The functionality of webbrowser is rather limited compared to the Python selenium module and its WebDriver automated testing framework for web applications. It is used for web scraping, domain-specific language scripting of remote browser interactivity and as an IDE for testing environments.

a. How to access the webbrowser module

Here we assume the most basic import scenario without aliasing. See our reference document on importing modules for more.

2. Python webbrowser Assignment

The main purpose for using assignment is to select a specific browser instead of the system default. If the default browser is sufficient, then skip to Part 3 on functions.

Below is syntax for creating a Browser Controller object with assignment followed by examples.

a. Assignment syntax
Syntax Priority
x = webbrowser.get(using=None)
Assignment will return a Browser Controller object listed here. This object can be used with methods that mirror the webbrowser.open(), webbrowser.new(), and webbrowser.new_tab() functions. The default of None will use the default browser or can be replaced with the browser type names below.
Below are browser type names and Class names.
  • 'mozilla' - Mozilla
  • 'firefox' - Mozilla
  • 'netscape' - Mozilla
  • 'galeon' - Galeon
  • 'epiphany' - Galeon
  • 'skipstone' - BackgroundBrowser
  • 'kfmclient' - Konqueror
  • 'konqueror' - Konqueror
  • 'kfm' - Konqueror
  • 'mosaic' - BackgroundBrowser
  • 'opera' - Opera
  • 'grail' - Grail
  • 'links' - GenericBrowser
  • 'elinks' - Elinks
  • 'lynx' - GenericBrowser
  • 'w3m' - GenericBrowser
  • 'windows-default' - WindowsDefault
  • 'macosx' - MacOSX
  • 'safari' - MacOSX
  • 'google-chrome' - Chrome
  • 'chrome' - Chrome
  • 'chromium' - Chromium
  • 'chromium-browser' - Chromium
Mid
b. Assignment examples

Here we demonstrate how to create a Browser Controller object labeled x.

>>> x = webbbrowser.get() # select default browser >>> type(x) <class 'webbrowser.BackgroundBrowser'> >>> x = webbbrowser.get('chrome') # Chrome browser >>> type(x) <class 'webbrowser.Chrome'> >>> x = webbrowser.get('firefox') # Firefox browser >>> type(x) <class 'webbrowser.Mozilla'> >>> x = webbrowser.get('windows-default') # select default on Windows >>> type(x) <class 'webbrowser.WindowsDefault'> >>> x.open("https://example.com") # open page with dot notation True

The last example demonstrates how to use standard dot notation to call the open() method. See Part 4 below on methods for more.

3. Python webbrowser Functions

The Python webbrowser functions can be called from Python scripts, the Python Interpreter or the command line. The primary function is webbrowser.open(). With its three parameters it can be customized and used in place of webbrowser.open_new() and webbrowser.open_new_tab(). This is up to user preference.

To select a browser other than the default make sure to read Parts 2 and 4 on methods because creating a Browser Controller object with assignment using the webbrowser.get() function is required.

a. Function syntax
Syntax Priority
webbrowser.open(url, new=0, autoraise=True)
The url is the only required parameter. The new=0 default opens in the same browser window and with new=1 a new browser window. For new=2 a new tab is opened. For the default autoraise=True the window will be raised into view.
High
webbrowser.open_new(url)
Opens the url in the default browser in a new window.
Low
webbrowser.open_new_tab(url)
Opens the url in the default browser in a new tab, or a new window if the browser is not already open.
Low
webbrowser.get(using=None)
Returns a Browser Controller object for the browser specified. Use this if you want to specify a browser other than the default. See the list of available browsers above. Here, when left blank or using using=None will use the default browser on the system. See the next function to register a new browser.
Mid
webbrowser.register(name, constructor, instance=None)
If a browser is not on the list of available options in the assignment section above it can be registered with two required arguments: name and constructor. Setting the BROWSER environment variable may be required to point to the browser's executable. See the official Python.org documentation for details.
Low
b. Function examples
The webbrowser.open() function

A call to the webbrowser.open() function with only a url by default opens in the same browser window with the defaults new=0 and autoraise=True.

The functions return True at the Python Interpreter upon success.

>>> webbrowser.open("https://example.com", new=0) True # opens in same window >>> webbrowser.open("https://example.com", 0) True # same as first example >>> webbrowser.open("https://example.com", 1) True # new window >>> webbrowser.open("https://example.com", 2, False) True # new tab in the background >>> webbrowser.open_new("https://example.com") True # new window >>> webbrowser.open_new_tab("https://example.com") True # new tab

In the last two examples we see the two alternatively named functions that perform similar functions as webbrowser.open().

You may find inconsistent behavior because the two parameters new=0 and autoraise=True, at times, are overridden by operating system settings.

The webbrowser.get() function

This function has two purposes. First without arguments it prints the default browser.

Second, to specify a different browser from the list supplied, this function creates a Browser Controller object.

>>> webbrowser.get() # print name of Windows default browser <webbrowser.WindowsDefault object at 0x23of8yne2o3uf> >>> webbrowser.get() # print name of Linux default browser <webbrowser.BackgroundBrowser object at 0x34vap3f93pf83> >>> webbrowser.get() # print name of macOS default browser <webbrowser.MacOSXOSAScript object at 0x3g2g0943h3uoi> >>> x = webbrowser.get('chrome') # create object with Chrome browser <webbrowser.Chrome object at 0x8i3oe03r1f05n>
Using the webbrowser module from the command line

Alternatively, webbrowser can be launched from the command line with -m webbrowser to select and run the module. The -n flag opens the URL in a new browser window. Alternatively, the -t option opens the URL in a new tab.

$ python3 -m webbrowser -n "https://example.com" # new window $ python3 -m webbrowser -t "https://example.com" # new tab

These commands will use the default browser settings on the system.

4. Python webbrowser Methods

As mentioned, methods are only necessary if your desire is to select a browser other than the system default. To access methods an assignment must be made to create a Browser Controller object (see Part 2 above).

a. Method syntax

The syntax below assumes that the object has been named x.

Syntax Priority
x.open(url, new=0, autoraise=True)
The url is the only required parameter. The new=0 default opens in the same browser window and with new=1 a new browser window. For new=2 a new tab is opened. For the default autoraise=True the window will be raised into view.
High
x.open_new(url)
Opens the url in the default browser in a new window.
Low
x.open_new_tab(url)
Opens the url in the default browser in a new tab, or a new window if the browser is not already open.
Low

Notice how these three methods mirror the functions from earlier.

b. Method examples

Here we create two instances of Browser Controller objects, one for the Chrome browser, named c and the second using Firefox named f. Here we open a web page with each.

>>> c = webbrowser.get('chrome') # select the Chrome browser >>> type(c) <class 'webbrowser.Chrome'> >>> c.open("https://example.com") # open in Chrome with dot notation True >>> f = webbrowser.get('firefox') # select the Firefox browser >>> type(f) <class 'webbrowser.Mozilla'> >>> f.open("https://example.com") # open in Firefox with dot notation True

As mentioned, some systems will override parameters, so you may see inconsistent behavior.

5. Find Local Help on Python webbrowser

To access local help on the Python webbrowser module help('webbrowser') at the Python Interpreter. Output for Python 3.5.3 looks like this.

Help on module webbrowser: NAME webbrowser - Interfaces for launching and remotely controlling Web browsers. MODULE REFERENCE https://docs.python.org/3.5/library/webbrowser The following documentation is automatically generated from the Python source file. It may be incomplete, incorrect or include features that are considered implementation detail and may vary between Python implementations. When in doubt, consult the module reference at the location listed above. (remainder trimmed)
Summary

In the end there is a lot of duplication across the three forms of functions to open web pages using Python webbrowser. Also, several parameters may be overridden anyway, so if you simply use webbrowser.open(url) for the default to open a page as a tab in the current browser and webbrowser.open(url, 1) to open a new window, that will probably get you most of the way there.

Also, we've seen how to select different browsers which can be helpful, but for a more thorough exploration of testing web applications, the selenium module offers more functionality.


Related Python Content


What's Next?

Subscribe to our growing YouTube Channel, a companion to this free online educational website. Follow @factorpad on Twitter for new content notifications.

Outline Back Tip Next

/ factorpad.com / tech / python / reference / python-webbrowser.html


python webbrowser
python webbrowser module
python open a web browser
python webbrowser open
python display web pages
python web programming
python webbrowser open new
examples of python webbrowser
python import webbrowser
python webbrowser get
open web browser from python
open browser from python
web scraping

A newly-updated free resource. Connect and refer a friend today.