Step By Step Code in Python to Open URL in Browser [Complete Guide]

Do you know how to open URL in web-browser using Python Program? If you are looking for the answer to this question, here in this tutorial. I am going to share simple commands in Python to open URL in browser. You can use.

First of all, you need to import webbrowser library. If the library is not installed on your system, you can install it using pip by the following command.

pip install webbrowser

If you are not sure if the webbrowser library installed or not, just don’t bother about it. Run the following program shared in this article.

If you get an error like…

ImportError: No module named webbrowser

this means you don’t have installed webbrowser library.

If it is not installed on your system, you can install it using pip by the following command.

import webbrowser
webbrowser.open_new('http://www.csestack.org')
# opens in default browser and in new window

Open URL in a new tab:

import webbrowser
webbrowser.open_new_tab('http://www.csestack.org')
# opens in default browser and in new tab

Exploring it Further…
If you want to explore more method available with the module webbrowser, you can get all the associated method by running dir() method.

import webbrowser
dir(webbrowser)

Use of webbrowser Python module for opening URL in the browser?

This is useful for automation testing in web development. Using this code you can open the web development website URL in the browser and then you can test. Clicking on buttons, filling the form automatically, login website and there are so many test cases you can explore with it. Selenium is one of the testing automation tools which have all these features.

You can use it for web scraping.

By using a single script, you can open the URL in multiple browsers. If you are into web development, you can run the script to check out rendering layout of a new website in multiple browsers.

How can you become Good Web developer Using Python?

If you are interested in Web Development or Automation, you can check out some of the interesting articles that may help you to explore your Python skill…

This all about this programming tutorial in Python to open URL in browser. I also mentioned some of the scenarios you can find this tutorial useful. If you have any doubt, head to the comment section below.

I am complete Python Nut, love Linux and vim as an editor. I hold Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack Portal.