• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

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

Aniruddha Chaudhari/169593/52
CodePython

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…

  • Create Your Own HTTP Server Using Python
  • Develop Your Own Website in Python

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.

  • How do I Use 10 Lines code of Python to Generate HTML Page?

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.

Python Interview Questions eBook

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

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    John
    July 20, 2017 at 3:21 pm

    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

    • Reply
      Aniruddha Chaudhari
      July 20, 2017 at 3:35 pm

      Hey John,

      Thanks for informing. Corrected!

      Happy to see you here.

  • Reply
    abdullah
    March 21, 2018 at 6:56 pm

    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?

    • Reply
      Aniruddha Chaudhari
      March 22, 2018 at 9:53 am

      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.

  • Reply
    Warren C
    April 2, 2018 at 9:07 pm

    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.

    • Reply
      Aniruddha Chaudhari
      April 2, 2018 at 10:28 pm

      Welcome, Warren!

      Hope you find some useful Python coding stuff on our portal.

      Happy Pythoning!

  • Reply
    Rosy Martin
    June 7, 2018 at 8:13 pm

    Thank you for this interesting post, I have shared it on Twitter.

    • Reply
      Aniruddha Chaudhari
      June 17, 2018 at 9:14 am

      Thanks, Rosy!

  • Reply
    Jaideep Samuel
    October 28, 2018 at 10:54 am

    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

    • Reply
      Aniruddha Chaudhari
      October 28, 2018 at 3:31 pm

      Jaideep, try using the “new” parameter with webbrowser.open(strURL, new=2) method.

  • Reply
    Mayank Chahar
    April 10, 2019 at 7:56 pm

    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?

    • Reply
      Aniruddha Chaudhari
      April 10, 2019 at 8:04 pm

      You have to use web scraping to find the URL inside the page. And then open the links in new tab recursively.

  • Reply
    Chakri
    May 9, 2019 at 3:44 pm

    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.

    • Reply
      Aniruddha Chaudhari
      May 9, 2019 at 8:58 pm

      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.

  • Reply
    Mark Ben
    May 11, 2019 at 9:16 am

    thank you Aniruddha, I’ve been searching all over for this!

  • Reply
    Surya Prakash Shah
    July 22, 2019 at 2:28 pm

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

    • Reply
      Aniruddha Chaudhari
      July 23, 2019 at 12:22 pm

      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.

  • Reply
    Mitesh
    July 27, 2019 at 2:38 pm

    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!

    • Reply
      Aniruddha Chaudhari
      July 27, 2019 at 5:52 pm

      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.

      • Reply
        Mitesh
        July 29, 2019 at 9:52 am

        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.

        • Reply
          Aniruddha Chaudhari
          July 29, 2019 at 10:02 am

          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.

  • Reply
    Mitesh
    July 30, 2019 at 9:39 am

    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?

    • Reply
      Aniruddha Chaudhari
      July 30, 2019 at 10:47 am

      This is strange, Mitesh. I did not try this with raspberry pi. I will try if I get any solution for this.

      • Reply
        Mitesh
        July 30, 2019 at 11:05 am

        Hamm… Ok… If you have any answer please, comment to me. Thanks a lot!!

        • Reply
          Aniruddha Chaudhari
          July 30, 2019 at 11:15 am

          Sure! Will update you if there is anything from me.

          • Mitesh
            August 3, 2019 at 2:25 pm

            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?

          • Aniruddha Chaudhari
            August 4, 2019 at 9:21 am

            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.

  • Reply
    ANKITA
    July 30, 2019 at 5:53 pm

    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.

    • Reply
      Aniruddha Chaudhari
      July 30, 2019 at 10:37 pm

      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.

    • Reply
      Om Parab
      October 15, 2020 at 5:49 pm

      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.

  • Reply
    Honey Chauhan
    September 15, 2019 at 1:45 pm

    HI Aniruddha Chaudhari,

    how to open multiple URLs by one click.

    • Reply
      Aniruddha Chaudhari
      September 15, 2019 at 2:02 pm

      Hi Honey,

      You can call webbrowser.open_new() in a loop.

      Example:

      listURL=['abc.com', 'xyz.com', 'pqr.com']
      for url in listURL:
          webbrowser.open_new(url)
      
      • Reply
        John Zeng
        October 29, 2019 at 9:05 pm

        hi, how do you know the website is open or not my friend?

        • Reply
          Aniruddha Chaudhari
          October 30, 2019 at 1:55 pm

          John, you can check the HTTP return code. If it is 404, website is not reachable.

      • Reply
        Ivan
        July 21, 2022 at 8:19 pm

        Thank you! This was great Aniruddha. Now, what if I want to open all urls from a file (csv)?

        • Reply
          Aniruddha Chaudhari
          July 30, 2022 at 12:16 pm

          This is very much possible, Ivan. Basically, you have to club the two tutorials.

          1. Reading CSV files into array list (https://www.csestack.org/how-to-read-csv-file-in-python/)
          2. Open URLs (This tutorial) by looping over the complete array list

  • Reply
    Ashutosh
    September 29, 2019 at 8:36 pm

    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?

    • Reply
      Aniruddha Chaudhari
      September 30, 2019 at 8:19 am

      You can do this with Selenium! There is a selenium module in Python. Install it using pip.

  • Reply
    Mesfin
    July 29, 2020 at 11:17 am

    Hi,
    I want to open a web page from Linux terminal.
    How can I do that?

    Thank you!

    • Reply
      Aniruddha Chaudhari
      August 1, 2020 at 1:58 pm

      Hey, use the following command in your Linux terminal.

      xdg-open www.example.com 

      It will open a given URL in your default browser.

  • Reply
    vinayn
    September 13, 2020 at 12:30 am

    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

    • Reply
      Aniruddha Chaudhari
      September 16, 2020 at 10:57 pm

      Hi Vinay,
      Can you share the exact error you are getting?

  • Reply
    Peter
    October 13, 2020 at 7:57 pm

    Hi Aniruddha!
    When I run it, nothing happens. How to fix that?

    • Reply
      Aniruddha Chaudhari
      October 17, 2020 at 8:25 am

      Hello Peter.

      It’s really difficult to identify what went wrong in your case. Did you get any console error?

  • Reply
    James Peterson
    April 23, 2021 at 10:53 am

    What’s the module name Mr. Chaudhari?

    • Reply
      Aniruddha Chaudhari
      April 23, 2021 at 10:55 am

      James, here we are using “webbrowser” Python module. You have to install it using pip tool.

  • Reply
    Venky
    May 28, 2021 at 6:58 pm

    Nice tutorial and very well explained.

  • Reply
    Jessica Lynt
    May 30, 2021 at 5:16 am

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

    • Reply
      Aniruddha Chaudhari
      May 30, 2021 at 7:58 pm

      Thanks, Jessica! It can not trace the TOR browser automatically. You can mention the executable path of the TOR browser in webbrowser.get() method.

  • Reply
    Shahzaib
    June 5, 2021 at 3:55 pm

    Sir, if I want to open a URL in chrome for all users then how can I do it?

  • Reply
    Gary Hornback
    September 22, 2022 at 8:36 pm

    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!

    • Reply
      Aniruddha Chaudhari
      October 4, 2022 at 3:37 pm

      Hi Garu, based on my understanding, what you want is that you want to call and open website url asynchronously. You can use Python async function call mechanism for it.

Leave a Reply Cancel reply

Basic Python Tutorial

  1. Python- Tutorial Overview
  2. Python- Applications
  3. Python- Setup on Linux
  4. Python- Setup on Windows
  5. Python- Basic Syntax
  6. Python- Variable Declaration
  7. Python- Numeric Data Types
  8. Python- NoneType
  9. Python- if-else/elif
  10. Python- for/while else
  11. Python- User Input
  12. Python- Multiline User Input
  13. Python- String Formatting
  14. Python- Find Substring in String
  15. Python- Bitwise Operators
  16. Python- Range Function
  17. Python- List
  18. Python- List Vs Tuple
  19. Python- Compare Two Lists
  20. Python- Sorting List
  21. Python- Delete Element from List
  22. Python- Dictionary
  23. Python- ‘is’ vs ‘==’
  24. Python- Mutable vs Immutable
  25. Python- Generator & Yield
  26. Python- Fibonacci Generator
  27. Python- Assert Statement
  28. Python- Exception Handling 
  29. Python- RegEx
  30. Python- Lambda Function
  31. Python- Installing Modules
  32. Python- Important Modules
  33. Python- Find all Installed Modules
  34. PyCharm- IDE setup
  35. Python- File Handling
  36. Python- Monkey Patching
  37. Python- Decorators
  38. Python- Instance vs Static vs Class Method
  39. Python- Name Mangling
  40. Python- Working with GUI
  41. Python- Read Data from Web URL
  42. Python- Memory Management
  43. Python- Virtual Environment
  44. Python- Calling C Function

Python Exercise

  1. Python- Tricky Questions
  2. Python- Interview Questions (60+)
  3. Python- Project Ideas (45+)
  4. Python- MCQ Test Online
  5. Python- Coding Questions (50+)
  6. Python- Competitive Coding Questions (20+)

Python String

  1. Reverse the String
  2. Permutations of String
  3. Padding Zeros to String/Number

Python List

  1. Randomly Select Item from List
  2. Find Unique Elements from List
  3. Are all Elements in List Same?

Python Dictionary

  1. Set Default Value in Dictionary
  2. Remove all 0 from a dictionary

File Handling

  1. Python- Read CSV File into List
  2. Check if the File Exist in Python
  3. Find Longest Line from File

Compilation & Byte Code

  1. Multiple Py Versions on System
  2. Convert .py file .pyc file
  3. Disassemble Python Bytecode

Algorithms

  1. Sorting- Selection Sort
  2. Sorting- Quick Sort

Other Python Articles

  1. Clear Py Interpreter Console
  2. Can I build Mobile App in Python?
  3. Extract all the Emails from File
  4. Python Shell Scripting

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard