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.

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.

webbrowser.open()

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.

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

result = webbrowser.get()
print(result)

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. 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().

<hr>

This marks the end of the Python Webbrowser guide. Use this link to go back to the main Python Libraries article.

Leave a Reply

Your email address will not be published. Required fields are marked *