The Wayback Machine - https://web.archive.org/web/20240115061453/https://www.geeksforgeeks.org/python-launch-a-web-browser-using-webbrowser-module/
Open In App
Related Articles

Python | Launch a Web Browser using webbrowser module

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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 browser window, if possible, and -t opens the URL in a new browser tab. 

Python




python -m webbrowser -t "https://www.google.com"


NOTE: webbrowser is part of the python standard library. Therefore, there is no need to install a separate package to use it.

The webbrowser module can be used to launch a browser in a platform-independent manner as shown below:
Code #1 : 

Python3




import webbrowser
webbrowser.open('http://www.python.org')


Output : 

True

This opens the requested page using the default browser. To have a bit more control over how the page gets opened, use one of the following functions given below in the code –
Code #2 : Open the page in a new browser window. 

Python3




webbrowser.open_new('http://www.python.org')


Output : 

True

Code #3 : Open the page in a new browser tab. 

Python3




webbrowser.open_new_tab('http://www.python.org')


Output : 

True

These will try to open the page in a new browser window or tab, if possible and supported by the browser. To open a page in a specific browser, use the webbrowser.get() function to specify a particular browser.
Code #4 : 

Python3




c = webbrowser.get('firefox')
 
c.open_new_tab('http://docs.python.org')


Output : 

True
True

Being able to easily launch a browser can be a useful operation in many scripts. For example, maybe a script performs some kind of deployment to a server and one would like to have it quickly launch a browser so one can verify that it’s working. Or maybe a program writes data out in the form of HTML pages and just like to fire up a browser to see the result. Either way, the webbrowser module is a simple solution.
 


Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 31 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials