• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1:mod:`!webbrowser` --- Convenient web-browser controller
2========================================================
3
4.. module:: webbrowser
5   :synopsis: Easy-to-use controller for web browsers.
6
7.. moduleauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
8.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
9
10**Source code:** :source:`Lib/webbrowser.py`
11
12--------------
13
14The :mod:`webbrowser` module provides a high-level interface to allow displaying
15web-based documents to users. Under most circumstances, simply calling the
16:func:`.open` function from this module will do the right thing.
17
18Under Unix, graphical browsers are preferred under X11, but text-mode browsers
19will be used if graphical browsers are not available or an X11 display isn't
20available.  If text-mode browsers are used, the calling process will block until
21the user exits the browser.
22
23If the environment variable :envvar:`BROWSER` exists, it is interpreted as the
24:data:`os.pathsep`-separated list of browsers to try ahead of the platform
25defaults.  When the value of a list part contains the string ``%s``, then it is
26interpreted as a literal browser command line to be used with the argument URL
27substituted for ``%s``; if the part does not contain ``%s``, it is simply
28interpreted as the name of the browser to launch. [1]_
29
30For non-Unix platforms, or when a remote browser is available on Unix, the
31controlling process will not wait for the user to finish with the browser, but
32allow the remote browser to maintain its own windows on the display.  If remote
33browsers are not available on Unix, the controlling process will launch a new
34browser and wait.
35
36On iOS, the :envvar:`BROWSER` environment variable, as well as any arguments
37controlling autoraise, browser preference, and new tab/window creation will be
38ignored. Web pages will *always* be opened in the user's preferred browser, in
39a new tab, with the browser being brought to the foreground. The use of the
40:mod:`webbrowser` module on iOS requires the :mod:`ctypes` module. If
41:mod:`ctypes` isn't available, calls to :func:`.open` will fail.
42
43The script :program:`webbrowser` can be used as a command-line interface for the
44module. It accepts a URL as the argument. It accepts the following optional
45parameters:
46
47* ``-n``/``--new-window`` opens the URL in a new browser window, if possible.
48* ``-t``/``--new-tab`` opens the URL in a new browser page ("tab").
49
50The options are, naturally, mutually exclusive.  Usage example::
51
52   python -m webbrowser -t "https://www.python.org"
53
54.. availability:: not WASI, not Android.
55
56The following exception is defined:
57
58
59.. exception:: Error
60
61   Exception raised when a browser control error occurs.
62
63The following functions are defined:
64
65
66.. function:: open(url, new=0, autoraise=True)
67
68   Display *url* using the default browser. If *new* is 0, the *url* is opened
69   in the same browser window if possible.  If *new* is 1, a new browser window
70   is opened if possible.  If *new* is 2, a new browser page ("tab") is opened
71   if possible.  If *autoraise* is ``True``, the window is raised if possible
72   (note that under many window managers this will occur regardless of the
73   setting of this variable).
74
75   Returns ``True`` if a browser was successfully launched, ``False`` otherwise.
76
77   Note that on some platforms, trying to open a filename using this function,
78   may work and start the operating system's associated program.  However, this
79   is neither supported nor portable.
80
81   .. audit-event:: webbrowser.open url webbrowser.open
82
83
84.. function:: open_new(url)
85
86   Open *url* in a new window of the default browser, if possible, otherwise, open
87   *url* in the only browser window.
88
89   Returns ``True`` if a browser was successfully launched, ``False`` otherwise.
90
91
92.. function:: open_new_tab(url)
93
94   Open *url* in a new page ("tab") of the default browser, if possible, otherwise
95   equivalent to :func:`open_new`.
96
97   Returns ``True`` if a browser was successfully launched, ``False`` otherwise.
98
99
100.. function:: get(using=None)
101
102   Return a controller object for the browser type *using*.  If *using* is
103   ``None``, return a controller for a default browser appropriate to the
104   caller's environment.
105
106
107.. function:: register(name, constructor, instance=None, *, preferred=False)
108
109   Register the browser type *name*.  Once a browser type is registered, the
110   :func:`get` function can return a controller for that browser type.  If
111   *instance* is not provided, or is ``None``, *constructor* will be called without
112   parameters to create an instance when needed.  If *instance* is provided,
113   *constructor* will never be called, and may be ``None``.
114
115   Setting *preferred* to ``True`` makes this browser a preferred result for
116   a :func:`get` call with no argument.  Otherwise, this entry point is only
117   useful if you plan to either set the :envvar:`BROWSER` variable or call
118   :func:`get` with a nonempty argument matching the name of a handler you
119   declare.
120
121   .. versionchanged:: 3.7
122      *preferred* keyword-only parameter was added.
123
124A number of browser types are predefined.  This table gives the type names that
125may be passed to the :func:`get` function and the corresponding instantiations
126for the controller classes, all defined in this module.
127
128+------------------------+-----------------------------------------+-------+
129| Type Name              | Class Name                              | Notes |
130+========================+=========================================+=======+
131| ``'mozilla'``          | ``Mozilla('mozilla')``                  |       |
132+------------------------+-----------------------------------------+-------+
133| ``'firefox'``          | ``Mozilla('mozilla')``                  |       |
134+------------------------+-----------------------------------------+-------+
135| ``'epiphany'``         | ``Epiphany('epiphany')``                |       |
136+------------------------+-----------------------------------------+-------+
137| ``'kfmclient'``        | ``Konqueror()``                         | \(1)  |
138+------------------------+-----------------------------------------+-------+
139| ``'konqueror'``        | ``Konqueror()``                         | \(1)  |
140+------------------------+-----------------------------------------+-------+
141| ``'kfm'``              | ``Konqueror()``                         | \(1)  |
142+------------------------+-----------------------------------------+-------+
143| ``'opera'``            | ``Opera()``                             |       |
144+------------------------+-----------------------------------------+-------+
145| ``'links'``            | ``GenericBrowser('links')``             |       |
146+------------------------+-----------------------------------------+-------+
147| ``'elinks'``           | ``Elinks('elinks')``                    |       |
148+------------------------+-----------------------------------------+-------+
149| ``'lynx'``             | ``GenericBrowser('lynx')``              |       |
150+------------------------+-----------------------------------------+-------+
151| ``'w3m'``              | ``GenericBrowser('w3m')``               |       |
152+------------------------+-----------------------------------------+-------+
153| ``'windows-default'``  | ``WindowsDefault``                      | \(2)  |
154+------------------------+-----------------------------------------+-------+
155| ``'macosx'``           | ``MacOSXOSAScript('default')``          | \(3)  |
156+------------------------+-----------------------------------------+-------+
157| ``'safari'``           | ``MacOSXOSAScript('safari')``           | \(3)  |
158+------------------------+-----------------------------------------+-------+
159| ``'google-chrome'``    | ``Chrome('google-chrome')``             |       |
160+------------------------+-----------------------------------------+-------+
161| ``'chrome'``           | ``Chrome('chrome')``                    |       |
162+------------------------+-----------------------------------------+-------+
163| ``'chromium'``         | ``Chromium('chromium')``                |       |
164+------------------------+-----------------------------------------+-------+
165| ``'chromium-browser'`` | ``Chromium('chromium-browser')``        |       |
166+------------------------+-----------------------------------------+-------+
167| ``'iosbrowser'``       | ``IOSBrowser``                          | \(4)  |
168+------------------------+-----------------------------------------+-------+
169
170Notes:
171
172(1)
173   "Konqueror" is the file manager for the KDE desktop environment for Unix, and
174   only makes sense to use if KDE is running.  Some way of reliably detecting KDE
175   would be nice; the :envvar:`!KDEDIR` variable is not sufficient.  Note also that
176   the name "kfm" is used even when using the :program:`konqueror` command with KDE
177   2 --- the implementation selects the best strategy for running Konqueror.
178
179(2)
180   Only on Windows platforms.
181
182(3)
183   Only on macOS.
184
185(4)
186   Only on iOS.
187
188.. versionadded:: 3.2
189   A new :class:`!MacOSXOSAScript` class has been added
190   and is used on Mac instead of the previous :class:`!MacOSX` class.
191   This adds support for opening browsers not currently set as the OS default.
192
193.. versionadded:: 3.3
194   Support for Chrome/Chromium has been added.
195
196.. versionchanged:: 3.12
197   Support for several obsolete browsers has been removed.
198   Removed browsers include Grail, Mosaic, Netscape, Galeon,
199   Skipstone, Iceape, and Firefox versions 35 and below.
200
201.. versionchanged:: 3.13
202   Support for iOS has been added.
203
204Here are some simple examples::
205
206   url = 'https://docs.python.org/'
207
208   # Open URL in a new tab, if a browser window is already open.
209   webbrowser.open_new_tab(url)
210
211   # Open URL in new window, raising the window if possible.
212   webbrowser.open_new(url)
213
214
215.. _browser-controllers:
216
217Browser Controller Objects
218--------------------------
219
220Browser controllers provide these methods which parallel three of the
221module-level convenience functions:
222
223
224.. attribute:: controller.name
225
226   System-dependent name for the browser.
227
228
229.. method:: controller.open(url, new=0, autoraise=True)
230
231   Display *url* using the browser handled by this controller. If *new* is 1, a new
232   browser window is opened if possible. If *new* is 2, a new browser page ("tab")
233   is opened if possible.
234
235
236.. method:: controller.open_new(url)
237
238   Open *url* in a new window of the browser handled by this controller, if
239   possible, otherwise, open *url* in the only browser window.  Alias
240   :func:`open_new`.
241
242
243.. method:: controller.open_new_tab(url)
244
245   Open *url* in a new page ("tab") of the browser handled by this controller, if
246   possible, otherwise equivalent to :func:`open_new`.
247
248
249.. rubric:: Footnotes
250
251.. [1] Executables named here without a full path will be searched in the
252       directories given in the :envvar:`PATH` environment variable.
253