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

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)

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.

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 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.

52 Comments

  1. 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

  2. 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?

  3. 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.

  4. 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

  5. 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?

  6. 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.

    1. 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.

  7. 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)?

  8. 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!

    1. 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.

      1. 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.

        1. 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.

  9. 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?

          1. Hi Aniruddha, If I use a pop-up of flash player in python code for the web browser. So, is it possible to develop this type of logic in the local IP camera?

          2. Mitesh, try to pop-up flash player through Python in your web browser. Once you are able to do that, I think it is very much possible to deal with IP camera through your browser.

  10. Hello, I want to open either Firefox or chrome, after opening any browser of my choice I want to open either Facebook or Gmail and want to pass my username and password. All I want to do using argument parser in Python.

    Can you please help me? I am new to this and stuck on coding.

    1. Hi Ankita,

      You can open the Facebook, Gmail and any other website in Firefox or Chrome by mentioning the website name in the API call as per the demonstration in this tutorial.

      Unfortunately, you can not pass a username and password. You can save your username and password in the browser itself. So whenever you open Facebook or Gmail, it will show your profile.

    2. Hi Ankita, My name is Om. I know a way to log in using G – Mail. In fact, I even know how to help you send a G – Mail directly through Python. However, I don’t know about Facebook.

      This is how to do it for G – Mail:-

      #(Copy the code from here)

      import smtplib (Install this module using pip)
      
      print("What to say?")
      
      def sendEmail(parameter_list):
          content = input()
          to = "G - Mail ID of whoever you want to send the G - Mail to"
          SendEmail(to, content)
          server = smtplib.SMTP('smtp.gmail.com', 587)
          server.ehlo()
          server.starttls()
          server.login('Your G - Mail ID', 'Your G - Mail password')
          server.sendmail('Your G - Mail ID', to, content)
          server.close()
      
      #This will print an error if there is one
      except Exception as e:
              print(e)
      

      Tell me if it works, please, and also if there are any errors.

  11. Hi

    I want to create a python script that opens a browser detect username and password on a webpage and then fills
    how can I do that?

  12. hello airudha ji

    this is Vinay.
    I’m facing the issue for “Web browser ” in help menu
    I have created a python Executable file using Pyinstaller Ubuntu16.04 and I have tested ubuntu18.04.
    here the UI working is good but the web browser is not working in 18.04.
    the “webbrowser –> URL” is working only 16.04

  13. Awesome tutorial! Can I open a URL automatically in TOR as well but just substitute ‘chrome’ or ‘firefox’ with ‘tor’ instead?

  14. Hello. I can access the website that I want, but when I get out Python doesn’t continue with the script. Do you have any examples of this? Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *