Skip to content
Related Articles

Related Articles

Improve Article

Python | Launch a Web Browser using webbrowser module

  • Last Updated : 12 Jun, 2019

In Python, webbrowser module provides a high-level interface which allows displaying Web-based documents to users. The webbrowser module can be used to launch a browser in a platform-independent manner as shown below:

Code #1 :




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.






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

Output :

True

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




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 :




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.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :