Step By Step Code in Python to Open URL in Browser [Complete Guide]
Do you know how to open URL in the 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 it.
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 executing the following command.
pip install webbrowser
Now you are ready to write your Python program to open any website URL in the web browser.
Let’s start writing the code with examples…
Code in Python to open URL in browser
How to open URL in browser Using Python?
Opening URL in Default Browser
# importing webbrowser python module import webbrowser #Assigning URL to be opened strURL = "http://www.csestack.org" #Open url in default browser webbrowser.open(strURL, new=2)
new parameter have special significance.
- If new = 0, open URL in same browser window
- If new = 1, opens URL in new browser window
- If new = 2, open URL in same tab.
Run this program.
If you don’t have webbrowser module installed on your system, you will get the following error.
ImportError: No module named webbrowser
In that case, you have to install the module using pip tool.
Opening URL in a specific browser, let’s say in Firefox and Chrome
import webbrowser webbrowser.get('firefox').open_new_tab('http://www.csestack.org') #Opens URL in Firefox browser webbrowser.get('chrome').open_new_tab('http://www.csestack.org') #Opens URL in Chrome browser
Open URL in the new browser window
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
import webbrowser dir(webbrowser)
Use of web browser 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 the rendering layout of a new website in multiple browsers. I use this trick while developing Django websites.
How can you become a 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…
I use Python for Web development many times. Below is a link that depicts a simple scenario to makes my web development activity so easy with Python.
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.