Webbrowser Module is used to manage the different URLs of web browsers and perform different functions associated with these URLs. Learn more about this module here.
Parameter Details
webbrowser.open()
url the URL to open in the web browser
new 0 opens the URL in the existing tab, 1 opens in a new window, 2 opens in new tab
autoraise if set to True, the window will be moved on top of the other windows
webbrowser.open_new()
url the URL to open in the web browser
webbrowser.open_new_tab()
url the URL to open in the web browser
webbrowser.get()
using the browser to use
webbrowser.register()
url browser name
constructor path to the executable browser (help)
instance An instance of a web browser returned from the method
webbrowser.get()
According to Python’s standard documentation, the webbrowser module provides a high-level interface to allow displaying Web-based documents to users. This topic explains and demonstrates proper usage of the webbrowser module.
Webbrowser Module: Opening a URL with Default Browser
To simply open a URL, use the webbrowser.open() method:
import webbrowser
webbrowser.open("https://stackoverflow.com")
If a browser window is currently open, the method will open a new tab at the specified URL. If no window is open, the method will open the operating system’s default browser and navigate to the URL in the parameter. The open method supports the following parameters:
url - the URL to open in the web browser (string) [required]
new – 0 opens in existing tab, 1 opens new window, 2 opens new tab (integer) [default 0] autoraise – if set to True, the window will be moved on top of other applications’ windows (Boolean)
[default False]
Note, the new and autoraise arguments rarely work as the majority of modern browsers refuse these commands.
Webbrowser can also try to open URLs in new windows with the open_new method:
import webbrowser
webbrowser.open_new("https://stackoverflow.com")
This method is commonly ignored by modern browsers and the URL is usually opened in a new tab. Opening a new tab can be tried by the module using the open_new_tab method:
import webbrowser
webbrowser.open_new_tab("https://stackoverflow.com")
Opening a URL with Di erent Browsers
The webbrowser module also supports different browsers using the register() and get() methods. The get method is used to create a browser controller using a specific executable’s path and the register method is used to attach these executables to preset browser types for future use, commonly when multiple browser types are used.
import webbrowser
ff_path = webbrowser.get("C:/Program Files/Mozilla Firefox/firefox.exe")
= webbrowser.get(ff_path)
ff.open("https://stackoverflow.com/")
Registering a browser type:
import webbrowser
ff_path = webbrowser.get("C:/Program Files/Mozilla Firefox/firefox.exe")
= webbrowser.get(ff_path)
webbrowser.register('firefox', None, ff)
Now to refer to use Firefox in the future you can use this webbrowser.get('firefox').open("https://stackoverflow.com/")
Must Read Python Interview Questions
200+ Python Tutorials With Coding Examples
Other Python Tutorials
- What is Python?
- Python Advantages
- Python For Beginners
- Python For Machine Learning
- Machine Learning For Beginners
- 130+ Python Projects With Source Code On GitHub