/ factorpad.com / tech / python / reference / python-webbrowser.html
An ad-free and cookie-free website.
Beginner
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
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.
webbrowser
/usr/lib/python3.5/webbrowser.py
import webbrowser
webbrowser.open()
Here we assume the most basic import scenario without aliasing. See our reference document on importing modules for more.
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.
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.
|
Mid |
Here we demonstrate how to create a Browser Controller object labeled
x
.
The last example demonstrates how to use standard dot notation to call
the open()
method. See Part 4 below on
methods for more.
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.
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 |
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.
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.
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.
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.
These commands will use the default browser settings on the system.
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).
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.
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.
As mentioned, some systems will override parameters, so you may see inconsistent behavior.
To access local help on the Python webbrowser module
help('webbrowser')
at the Python
Interpreter. Output for Python 3.5.3 looks like this.
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.
Subscribe to our growing YouTube Channel, a companion to this free online educational website. Follow @factorpad on Twitter for new content notifications.
/ factorpad.com / tech / python / reference / python-webbrowser.html
A newly-updated free resource. Connect and refer a friend today.