Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python Script to Open a Web Browser

  • Last Updated : 28 Nov, 2021

In this article we will be discussing some of the methods that can be used to open a web browser (of our choice) and visit the URL we specified, using python scripts.

In the Python package, we have a module named webbrowser, which includes a lot of methods that we can use to open the required URL in any specified browser we want. For that, we just have to import this module to our script file, and then we have to call some of its functions (declared and defined below) with our required inputs. Hence this module will then open up the browser we want and will get the page we want.

The methods present in this module are described below:

S No.Syntax of MethodDescription
1webbrowser.open(url, new = 0, autoraise = true)This is the main method where the web browser with the passed URL is opened and is displayed to the user. If the parameter “new” is 0 then URL is opened in the same browser and if it is 1 then URL is opened in another browser and if it’s 2, then the page is opened in another tab.
2webbrowser.open_new(url) URL passed is opened in the a new browser if it is possible to do that, else it is opened in default one.
3webbrowser.open_new_tab(url)Opens new tab of passpage URL passed in the browser which is currently active.
4webbrowser.get(using=None)This command is used to get the object code for the web browser we want to use. In simple words,, we could use this command to get the code of the web browser (stored in python) and then we could use that code to open that particular web browser. We passes the name of the web browser which we want to use as a string.
5webbrowser.register(name, constructor, instance=None, preferred=False)This method is used to register the name of the favorite browser in the Python environment if its code was not registered previously. Actually, at the beginning, none of the browsers is registered and only the default one is called each time. Hence we had to register them manually.

Now we are going to use these methods to see how we could open browsers with our passed URLs. 

Below is the implementation:

We have used an URL = https://www.geeksforgeeks.org, for this experiment. Our python script is stored in a file named geeks.py. Below is the code which we had written and implemented in it.

Example 1: Basic example of Open Web Browser in Python Script

Python3




# first import the module
import webbrowser
  
# then make a url variable
  
# then call the default open method described above
webbrowser.open(url)

Hence as a result the website got opened in the default web browser. As in mine, it is opened in Microsoft Edge as shown below:

Output:- Site opened in Edge

Example 2: Specify the browser

Python3




# first import the module
import webbrowser
  
# then make a url variable
  
# then call the get method to select the code 
# for new browser and call open method 
# described above
webbrowser.get('chrome').open(url)
  
# results in error since chrome is not registered initially.

Run the command in the prompt by typing “python geeks.py” and it will give the results. If we will try to open the browser of our choice without registering it for the first time then we will get the below-given type of response as an output.

Output if we don’t register the browser initially

Hence below given is the code which we modified so that it now registers the browsers and then opens that URL in it.

Example 3: Register the new browser

Python3




# first import the module
import webbrowser
  
# then make a url variable
  
# getting path
chrome_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
  
# First registers the new browser
webbrowser.register('chrome', None
                    webbrowser.BackgroundBrowser(chrome_path))
  
# after registering we can open it by getting its code.
webbrowser.get('chrome').open(url)

The output of using this command in the prompt is the same as given above but only the method is different.

Using python scripts in prompt.

Hence after executing it will open the chrome web browser.

There is one other method also for opening the browser using webbrowser in python. In this method, we don’t have to write the whole script and interpret it to open the browsers. We could just use python shell to execute a single command (given below) to open up the browser (default one) with a specified URL.

The shell command is given as:-

python -m webbrowser -t “https://www.geeksforgeeks.org”

Hence we saw two different methods and explained to them how to open a web browser using python scripts.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!