Python webbrowser

The Python webbrowser module provides a high-level interface to allow displaying Web-based documents to users. This guide will go through how to use this module to display and manipulate web pages.

Installation

You should check to see if you have the webbrowser module installed. Simply use the import webbrowser statement. You should likely already have this module as it is part of the Python Standard Library (and comes bundled with Python).

import webbrowser

Download and install it through the command prompt if you do not have it. Refer to our Getting Started Guide if you need help.


Opening a Web Browser

The webbrowser.open() function is used to open and display a website in your default browser. See the examples below.

import webbrowser

webbrowser.open("https://google.com")
import webbrowser

webbrowser.open("https://google.com", new = 2, autoraise = True)
ParameterDescription
urlThe url of the web page you wish to open using the default browser.
new If new is 0, the url is opened in the same browser window if possible. If new is 1, a new browser window is opened if possible. If new is 2, a new browser page (“tab”) is opened if possible.
autoraise If autoraise is True, the window is raised if possible. The term “raise” means to bring it to the front.


If you do not want the default browser to show, or you do not have a default browser, you can explicitly mention which browser you want.

import webbrowser

browser = webbrowser.Mozilla("C:\\Program Files\\Mozilla Firefox\\firefox.exe")
browser.open("https://google.com")

Double backslashes in the file path are required for windows only. Linux and macOS will only require one.


Other Functions in the Webbrowser Module


webbrowser.open_new(url)

Open the url in a new window of the default browser, if possible, otherwise, open the url in the only browser window.

import webbrowser

webbrowser.open_new(""https://google.com")

webbrowser.open_new_tab(url)

Open url in a new page (“tab”) of the default browser, if possible, otherwise it is equivalent to open_new().

import webbrowser

webbrowser.open_new_tab(""https://google.com")

webbrowser.get([name])

Returns a controller object for the browser type name. If name is empty, returns a controller object for the appropriate default browser. The controller object for a browser allows you to perform functions on it. See list of all browser names below.

import webbrowser

controller = webbrowser.get("chrome")
print(controler)

webbrowser.register()

It is used to register a browser name. Once a browser name has been registered, it’s controller object can be called using the get() function.

No.Type Name Class Name
1'mozilla'Mozilla('mozilla')
2'firefox'Mozilla('mozilla')
3'netscape'Mozilla('netscape')
4'galeon'Galeon('galeon')
5'epiphany'Galeon('epiphany')
6'skipstone'BackgroundBrowser('skipstone')
7'kfmclient'Konqueror()
8'konqueror'Konqueror()
9'kfm'Konqueror()
10'mosaic'BackgroundBrowser('mosaic')
11'opera'Opera()
12'grail'Grail()
13'links'GenericBrowser('links')
14'elinks'Elinks('elinks')
15'lynx'GenericBrowser('lynx')
16'w3m'GenericBrowser('w3m')
17'windows-default'WindowsDefault
18'macosx'MacOSX('default')
19'safari'MacOSX('safari')
20'google-chrome'Chrome('google-chrome')
21'chrome'Chrome('chrome')
22'chromium'Chromium('chromium')
23'chromium-browser'Chromium('chromium-browser')

Number 17 is only available on Windows.
Number 18 and 19 are only available on Mac OS.


Controller Functions

If you’ve called a controller object using the get function you can use the following commands with it.

controller.open(urlnew=0autoraise=True): Display url using the browser handled by this controller. If new is 1, a new browser window is opened if possible. Otherwise if new is 2, a new browser page (“tab”) is opened if possible.

controller.open_new(url): Open url in a new window of the browser handled by this controller, if possible, otherwise, open url in the only browser window. Alias open_new().

controller.open_new_tab(url): Open url in a new page (“tab”) of the browser handled by this controller, if possible, otherwise equivalent to open_new().


Check out other libraries that Python webbrowser is often used together with:


This marks the end of the Python Webbrowser guide. Use this link to go back to the main Python Libraries article. Questions regarding the tutorial can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments