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
If it is not installed on your system, you can install it using pip by the following command.
pip install webbrowser
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.
- Opening URL in 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 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 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…
I use Python for Web development many times. Below is a link that depicts 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.