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 Method | Description |
---|
1 | webbrowser.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. |
2 | webbrowser.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. |
3 | webbrowser.open_new_tab(url) | Opens new tab of passpage URL passed in the browser which is currently active. |
4 | webbrowser.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. |
5 | webbrowser.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
url = "https://www.geeksforgeeks.org"
# 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 EdgeExample 2: Specify the browser
Python3
# first import the module
import webbrowser
# then make a url variable
url = "https://www.geeksforgeeks.org"
# 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
url = "https://www.geeksforgeeks.org"
# 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.
Similar Reads
How to Run a Python Script
Python scripts are Python code files saved with a .py extension. You can run these files on any device if it has Python installed on it. They are very versatile programs and can perform a variety of tasks like data analysis, web development, etc. You might get these Python scripts if you are a begin
6 min read
How to Build Web scraping bot in Python
In this article, we are going to see how to build a web scraping bot in Python. Web Scraping is a process of extracting data from websites. A Bot is a piece of code that will automate our task. Therefore, A web scraping bot is a program that will automatically scrape a website for data, based on our
8 min read
How to use a Web Browser
A Web Browser is a software used to view websites over the internet. Some commonly used browsers are Microsoft Edge, Google Chrome, Opera, and Mozilla Firefox. To understand how to use a browser we will consider Chrome browser as an example to show various things that can be done in the browser.Here
7 min read
Python | Launch a Web Browser using webbrowser module
In Python, webbrowser module is a convenient web browser controller. It provides a high-level interface that allows displaying Web-based documents to users. webbrowser can also be used as a CLI tool. It accepts a URL as the argument with the following optional parameters: -n opens the URL in a new
2 min read
What is a Browser and How does it Work?
The web browser is an application software used to explore the World Wide Web (WWW). It acts as a platform that allows users to access information from the Internet by serving as an interface between the client (user) and the server. The browser sends requests to servers for web documents and servic
4 min read
How to create a PHP detection browser script ?
A browser script is used to find the complete information of the web browser. A web browser is working on the hypertext transfer protocol and retrieves all the information on the internet and displays it on your desktop so that you can access all the things from anywhere. In this article, we will s
2 min read
Python script to open a Google Map location on clipboard
The task is to create a python script that would open the default web browser to the Google map of the address given as the command line argument. Following is the step by step process:Â Creating the Address_string from command line input :Â Command line arguments can be read through sys module. The
3 min read
Controlling the Web Browser with Python
In this article, we are going to see how to control the web browser with Python using selenium. Selenium is an open-source tool that automates web browsers. It provides a single interface that lets you write test scripts in programming languages like Ruby, Java, NodeJS, PHP, Perl, Python, and C#, et
3 min read
How to setup Notepad to run Python Script
Due to its simplicity, versatility, and readability, Python has become phenomenally popular as a computer language that is highly leveled and dynamically typed. These are some of the IDEs for Python development that have many tools; however, if you are just starting or want it lighter, then Windowsâ
2 min read
How to scrape the web with Playwright in Python
In this article, we will discuss about Playwright framework, Its feature, the advantages of Playwright, and the Scraping of a basic webpage. The playwright is a framework for Web Testing and Automation. It is a fairly new web testing tool from Microsoft introduced to let users automate webpages more
3 min read