The webbrowser module, which comes preinstalled with Python, is a library that can be used to control web browsers. This article will show how this module can be used to open tabs and windows on installed browsers. You will get to choose the browser to use, whether to open the URL you want to visit on a new window or not and whether to open a new tab.
Open URL with Default Browser
The webbrowser library provides different ways of opening a URL on a browser through the open() functions of the module. The general syntax is:
webbrowser.open(url, new=0, autoraise=True)
Where new takes values 0, 1, or 2. The value 0 means the url is opened on the same browser window if the browser is open, 1 opens url on a new browser window, and 3 means that a new tab is opened.
If you want to open a new window, you can use the open_new() function as follows,
webbrowser.open_new(url)
If you want to open a new tab, open_new_tab() comes in handy. Note that, open_new_tab() reduces to open_new() if the default browser is not open already.
webbrowser.open_new_tab(url)
Example
The following code snippet opens the Wikipedia page in a new browser window. In my case, Firefox is opened because it is the default browser. A different browser might be opened for you if you have another default browser.
1 2 3 4 |
import webbrowser url = "https://www.wikipedia.org/" webbrowser.open(url, new=1, autoraise=True) |
Opening URL with a Given Browser
The webbrowser package also allows the user to specify the browser to open a URL using the get() function.
webbrowser.get(using=”location of the binary/executable”).open(URL)
When we specify the controller/browser we can use the same functions explained in the first section, that is, open(), open_new(), and open_new_tab(). I am running Linux Debian 11, and I have three browsers installed: Chromium, Google Chrome, and Firefox ESR. The following example shows how to open google.com on the Google Chrome browser.
1 2 3 4 |
import webbrowser Chrome = '/usr/bin/google-chrome-stable %s' webbrowser.get('/usr/bin/google-chrome-stable %s').open('www.google.com',new=1) |
The path /usr/bin/google-chrome-stable points to the Google Chrome binary named “google-chrome-stable”. We can pass google-chrome-stable instead of the full path, and webbrowser.get() will look for the binary at /usr/bin and /usr/local/bin. Binaries can be located in Linux using the command whereis as shown below
Here are more examples
1 2 3 4 5 6 7 8 |
import webbrowser # Go to google.com on Google Chrome controller = webbrowser.get('google-chrome-stable %s') controller.open('www.google.com') # Open Wikipedia on Chromium webbrowser.get("chromium %s").open("<a href="http://www.wikipedia.com">www.wikipedia.com</a>") |
Note: The process of opening URLs happens sequentially and not simultaneously.
Based on the operating system you are running; you may find Chrome browser in the following locations:
- Linux: chrome_path =”/usr/bin/google-chrome- %s“
- MacOS: chrome_path = “open -a /Applications/Google\ Chrome.app %s“
- Windows: chrome_path = “C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s“
Running webbrowser on the Terminal/ Command Line
The webbrowser is also available as a package module, which can be executed from the command line using the “m” option. The general syntax for running a module from the command line is given by
python -m <module_name> [OPTIONS]
To open the python.org website from the command line using the Python webbrowser, we will run the command:
1 |
python -m webbrowser "<a href="https://www.python.org">https://www.python.org</a>" |
The OPTIONS provided by the module include -n to open the URL in a new browser window, if possible; -t to open the URL in a new browser tab. The two options are mutually exclusive. If you use both, the first one to be issued takes precedence. The following line opens Wikipedia in a new tab if the browser is already open.
1 |
python -m webbrowser -t "https://www.wikipedia.com" |