- Step By Step Code in Python to Open URL in Browser [Complete Guide]
- Code in Python to open URL in browser
- Opening URL in Default Browser
- Opening URL in a specific browser, let’s say in Firefox and Chrome
- Open URL in the new browser window
- Open URL in a new tab
- Use of web browser Python module for opening URL in the browser?
- Comments
- webbrowser — Convenient web-browser controller¶
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.
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)
The 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.
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 dir() method.
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.
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a 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.org portal.
Comments
John
Hi Aniruddha, Just want to mention the copy paste error that crept in. webbrowser.get(‘firefox’).open_new_tab(‘http://www.csestack.org’)
#Opens URL in Chrome browser –John
Aniruddha Chaudhari
abdullah
Hi
I want to subscribe to the weekly newsletter
How do I open a link in Python?
I have a rest program It plays my favorite music
For example, I want the program to open the home page on YouTube, Then selects the music randomly.
How to do it?
Aniruddha Chaudhari
Hi Abdullah, You can subscribe here for a newsletter. Make a get request to Youtube webpage and use web scraping to read and manipulate the content.
Warren C
Hello Aniruddha thanks for sharing that vase knowledge of information. To be I always wanted to learn a little Python programming but haven’t to it yet.
Aniruddha Chaudhari
Rosy Martin
Aniruddha Chaudhari
Jaideep Samuel
I am seeing that both webbrowser.open_new & webbrowser.open_new_tab are opening new tabs in Google chrome. I am running this in Windows 10 environment
Aniruddha Chaudhari
Mayank Chahar
What to do if we want our browser to open by itself and then randomly visit a webpage. Also if there is another URL present inside that page, it should also be visited?
Aniruddha Chaudhari
You have to use web scraping to find the URL inside the page. And then open the links in new tab recursively.
Chakri
Hi Aniruddha.. Thanks for sharing the information. really helpful.
I have a task and need help for some ideas and at the same i am new to coding.
How to retrieve the new release version or service pack from any website? for example, MS power BI is releasing different versions and i would like see some notification through a python program. Can you help me out? Thanks,
Chakri.
Aniruddha Chaudhari
Hi Chakri, If you want to get the information (like MS power BI versions) from others website, you have to learn web scraping. You can write a Python script to call that website and collect and parse the data to find the release date. You can run that Python script after some time interval.
Mark Ben
Surya Prakash Shah
Hi Anirudh, The code is below: import webbrowser
URL=”https://www.facebook.com/”
webbrowser. get(‘chrome’). open(ULR) Error I got: File “C:\Python27\lib\webbrowser.py”, line 52, in get
raise Error(“could not locate runnable browser”)
Error: could not locate runnable browser Do I need to register/locate the Browser (It is not default browser)?
Aniruddha Chaudhari
Hi Surya, Looks like your system is not detecting the chrome browser to open. Try… 1. press “Win+R”
2. type “chrome” And check if it is opening the Chrome browser.
Mitesh
Hi, I am new in Python skill. I want to play IP camera in my raspberry pi’s web browser automatically using Python as it is firefox but when it’s open only blank page gets open, nothing to display. If it is possible please, let me inform. Thanks!
Aniruddha Chaudhari
Hi Mitesh, it’s really difficult to debug without knowing your code and implementation. You can try opening different web browsers like Chrome. For Raspberry pi, you can seek help from micro Python.
Mitesh
Thanks for your warm response Aniruddha… I am using selenium python for this IP camera & I am getting output with opening my web browser as chrome/firefox but every time I have to gives a allow permission to that web browser (only in chrome, firefox auto start but getting a white blank page). So, Here I have to gives flash permission in my web browser. I am trying but not getting this.
Aniruddha Chaudhari
Mitesh, for now, you can uninstall and Firefox so that you can test in chrome browser. And also check, is your firefox a default web browser? Specify browser name while opening tab, so it will open specified browser instead of default one.
Mitesh
Yes…you’re right but in chrome, with raspberry pi 3 I have come to some unknown error that … Checking for linux32 chromedriver:72.0.3626.69 in cache
There is no cached driver. Downloading new one…
Trying to download new driver from http:// chromedriver (dot) storage.googleapis.com/72.0.3626.69/chromedriver_linux32.zip
Traceback (most recent call last):
File “chroflash.py”, line 11, in
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), chrome_options=options)
File “/usr/local/lib/python3.5/dist-packages/webdriver_manager/chrome.py”, line 19, in install
bin_file = self._file_manager.download_driver(self.driver, path)
File “/usr/local/lib/python3.5/dist-packages/webdriver_manager/cache.py”, line 75, in download_driver
zip_file = self._download_file(driver, path)
File “/usr/local/lib/python3.5/dist-packages/webdriver_manager/cache.py”, line 99, in _download_file
driver.name, driver.get_version(), driver.get_url()))
ValueError: There is no such driver chromedriver with version 72.0.3626.69 by http:// chromedriver (dot) storage.googleapis.com/72.0.3626.69/chromedriver_linux32.zip Because of this error, I have many issues in chrome. So, Is there any option to auto allow permission in a web browser?
Aniruddha Chaudhari
This is strange, Mitesh. I did not try this with raspberry pi. I will try if I get any solution for this.
webbrowser — Convenient web-browser controller¶
The webbrowser module provides a high-level interface to allow displaying web-based documents to users. Under most circumstances, simply calling the open() function from this module will do the right thing.
Under Unix, graphical browsers are preferred under X11, but text-mode browsers will be used if graphical browsers are not available or an X11 display isn’t available. If text-mode browsers are used, the calling process will block until the user exits the browser.
If the environment variable BROWSER exists, it is interpreted as the os.pathsep -separated list of browsers to try ahead of the platform defaults. When the value of a list part contains the string %s , then it is interpreted as a literal browser command line to be used with the argument URL substituted for %s ; if the part does not contain %s , it is simply interpreted as the name of the browser to launch. 1
For non-Unix platforms, or when a remote browser is available on Unix, the controlling process will not wait for the user to finish with the browser, but allow the remote browser to maintain its own windows on the display. If remote browsers are not available on Unix, the controlling process will launch a new browser and wait.
The script webbrowser can be used as a command-line interface for the module. It accepts a URL as the argument. It accepts the following optional parameters: -n opens the URL in a new browser window, if possible; -t opens the URL in a new browser page (“tab”). The options are, naturally, mutually exclusive. Usage example:
python -m webbrowser -t "https://www.python.org"
This module does not work or is not available on WebAssembly platforms wasm32-emscripten and wasm32-wasi . See WebAssembly platforms for more information.
The following exception is defined:
exception webbrowser. Error ¶
Exception raised when a browser control error occurs.
The following functions are defined:
webbrowser. open ( url , new = 0 , autoraise = True ) ¶
Display url using the default browser. If new is 0, the url is opened in the same browser window if possible. If new is 1, a new browser window is opened if possible. If new is 2, a new browser page (“tab”) is opened if possible. If autoraise is True , the window is raised if possible (note that under many window managers this will occur regardless of the setting of this variable).
Note that on some platforms, trying to open a filename using this function, may work and start the operating system’s associated program. However, this is neither supported nor portable.
Raises an auditing event webbrowser.open with argument url .
Open url in a new window of the default browser, if possible, otherwise, open url in the only browser window.
webbrowser. open_new_tab ( url ) ¶
Open url in a new page (“tab”) of the default browser, if possible, otherwise equivalent to open_new() .
webbrowser. get ( using = None ) ¶
Return a controller object for the browser type using. If using is None , return a controller for a default browser appropriate to the caller’s environment.
webbrowser. register ( name , constructor , instance = None , * , preferred = False ) ¶
Register the browser type name. Once a browser type is registered, the get() function can return a controller for that browser type. If instance is not provided, or is None , constructor will be called without parameters to create an instance when needed. If instance is provided, constructor will never be called, and may be None .
Setting preferred to True makes this browser a preferred result for a get() call with no argument. Otherwise, this entry point is only useful if you plan to either set the BROWSER variable or call get() with a nonempty argument matching the name of a handler you declare.
Changed in version 3.7: preferred keyword-only parameter was added.
A number of browser types are predefined. This table gives the type names that may be passed to the get() function and the corresponding instantiations for the controller classes, all defined in this module.