webbrowser — Convenient web-browser controller¶
Source code:Lib/webbrowser.py
Thewebbrowser module provides a high-level interface to allow displayingweb-based documents to users. Under most circumstances, simply calling theopen() function from this module will do the right thing.
Under Unix, graphical browsers are preferred under X11, but text-mode browserswill be used if graphical browsers are not available or an X11 display isn’tavailable. If text-mode browsers are used, the calling process will block untilthe user exits the browser.
If the environment variableBROWSER exists, it is interpreted as theos.pathsep-separated list of browsers to try ahead of the platformdefaults. When the value of a list part contains the string%s, then it isinterpreted as a literal browser command line to be used with the argument URLsubstituted for%s; if the value is a single word that refers to one of thealready registered browsers this browser is added to the front of the search list;if the part does not contain%s, it is simply interpreted as the name of thebrowser to launch.[1]
Changed in version 3.14:TheBROWSER variable can now also be used to reorder the list ofplatform defaults. This is particularly useful on macOS where the platformdefaults do not refer to command-line tools onPATH.
For non-Unix platforms, or when a remote browser is available on Unix, thecontrolling process will not wait for the user to finish with the browser, butallow the remote browser to maintain its own windows on the display. If remotebrowsers are not available on Unix, the controlling process will launch a newbrowser and wait.
On iOS, theBROWSER environment variable, as well as any argumentscontrolling autoraise, browser preference, and new tab/window creation will beignored. Web pages willalways be opened in the user’s preferred browser, ina new tab, with the browser being brought to the foreground. The use of thewebbrowser module on iOS requires thectypes module. Ifctypes isn’t available, calls toopen() will fail.
Command-line interface¶
The scriptwebbrowser can be used as a command-line interface for themodule. It accepts a URL as the argument. It accepts the following optionalparameters:
- -n,--new-window¶
Opens the URL in a new browser window, if possible.
- -t,--new-tab¶
Opens the URL in a new browser tab.
The options are, naturally, mutually exclusive. Usage example:
python-mwebbrowser-t"https://www.python.org"Availability: not WASI, not Android.
The following exception is defined:
- exceptionwebbrowser.Error¶
Exception raised when a browser control error occurs.
The following functions are defined:
- webbrowser.open(url,new=0,autoraise=True)¶
Displayurl using the default browser. Ifnew is 0, theurl is openedin the same browser window if possible. Ifnew is 1, a new browser windowis opened if possible. Ifnew is 2, a new browser page (“tab”) is openedif possible. Ifautoraise is
True, the window is raised if possible(note that under many window managers this will occur regardless of thesetting of this variable).Returns
Trueif a browser was successfully launched,Falseotherwise.Note that on some platforms, trying to open a filename using this function,may work and start the operating system’s associated program. However, thisis neither supported nor portable.
Raises anauditing event
webbrowser.openwith argumenturl.
- webbrowser.open_new(url)¶
Openurl in a new window of the default browser, if possible, otherwise, openurl in the only browser window.
Returns
Trueif a browser was successfully launched,Falseotherwise.
- webbrowser.open_new_tab(url)¶
Openurl in a new page (“tab”) of the default browser, if possible, otherwiseequivalent to
open_new().Returns
Trueif a browser was successfully launched,Falseotherwise.
- webbrowser.get(using=None)¶
Return a controller object for the browser typeusing. Ifusing is
None, return a controller for a default browser appropriate to thecaller’s environment.
- webbrowser.register(name,constructor,instance=None,*,preferred=False)¶
Register the browser typename. Once a browser type is registered, the
get()function can return a controller for that browser type. Ifinstance is not provided, or isNone,constructor will be called withoutparameters to create an instance when needed. Ifinstance is provided,constructor will never be called, and may beNone.Settingpreferred to
Truemakes this browser a preferred result foraget()call with no argument. Otherwise, this entry point is onlyuseful if you plan to either set theBROWSERvariable or callget()with a nonempty argument matching the name of a handler youdeclare.Changed in version 3.7:preferred keyword-only parameter was added.
A number of browser types are predefined. This table gives the type names thatmay be passed to theget() function and the corresponding instantiationsfor the controller classes, all defined in this module.
Type Name | Class Name | Notes |
|---|---|---|
|
| |
|
| |
|
| |
|
| (1) |
|
| (1) |
|
| (1) |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| (2) |
|
| (3) |
|
| (3) |
|
| |
|
| |
|
| |
|
| |
|
| (4) |
Notes:
“Konqueror” is the file manager for the KDE desktop environment for Unix, andonly makes sense to use if KDE is running. Some way of reliably detecting KDEwould be nice; the
KDEDIRvariable is not sufficient. Note also thatthe name “kfm” is used even when using thekonqueror command with KDE2 — the implementation selects the best strategy for running Konqueror.Only on Windows platforms.
Only on macOS.
Only on iOS.
Added in version 3.2:A newMacOSXOSAScript class has been addedand is used on Mac instead of the previousMacOSX class.This adds support for opening browsers not currently set as the OS default.
Added in version 3.3:Support for Chrome/Chromium has been added.
Changed in version 3.12:Support for several obsolete browsers has been removed.Removed browsers include Grail, Mosaic, Netscape, Galeon,Skipstone, Iceape, and Firefox versions 35 and below.
Changed in version 3.13:Support for iOS has been added.
Here are some simple examples:
url='https://docs.python.org/'# Open URL in a new tab, if a browser window is already open.webbrowser.open_new_tab(url)# Open URL in new window, raising the window if possible.webbrowser.open_new(url)
Browser controller objects¶
Browser controllers provide thename attribute,and the following three methods which parallel module-level convenience functions:
- controller.name¶
System-dependent name for the browser.
- controller.open(url,new=0,autoraise=True)¶
Displayurl using the browser handled by this controller. Ifnew is 1, a newbrowser window is opened if possible. Ifnew is 2, a new browser page (“tab”)is opened if possible.
- controller.open_new(url)¶
Openurl in a new window of the browser handled by this controller, ifpossible, otherwise, openurl in the only browser window. Alias
open_new().
- controller.open_new_tab(url)¶
Openurl in a new page (“tab”) of the browser handled by this controller, ifpossible, otherwise equivalent to
open_new().
Footnotes
[1]Executables named here without a full path will be searched in thedirectories given in thePATH environment variable.