How do I Open Web Browser in Python

how to open browser

Python webbrowser module can help us open a web browser for us. You can open chrome, safari, and any other browser with the help of the python programming language.

webbrowser is a python module and is known as the convenient web-browser controller. This python module provides a very high-level interface to allow displaying Web-based documents to users.

How to Open Web browser with Python?

To open a web browser with Python, simply use the webbrowser.open_new(url) method. This method is available in the webbrowser module. You just have to provide only one parameter which is the URL.

The Url of the page that you want to display in the browser should be provided to the module in python. IN the following example we are opening a link. In the following example, the URL which is "" will be open in a Microsoft-edge old version. So this is a good choice to go for if you do not if the user has some other browser installed. This will not fail you as Microsoft edge is installed in every windows operating system.

See the Following Code Example:
import  logging
import webbrowser, threading

def run():
    # load()
    url = """Starting browser at")
    # webbrowser.open_new(url)
    threading.Timer(1.25, lambda: ).start()

How to Open a link in a new tab with Python?

To open a link in a new tab you can use the webbrowser.open_new_tab(url) method. This method works only if there is already a web browser opened by the controller, otherwise it same as the webbrowser.open_new(url).

In the following example, we have created a function that will see if there is already a web browser opened by the controller or not. if the browser is opened by the controller it will then simply open a new tab, otherwise, it will open a new browser window for us.

See the Following Python 3 Code Example:
import webbrowser
new_tab = True
def open_browser(link):
    global new_tab
    if new_tab is False:

How to open a file in a web Browser with Python?

You can use the webbrowser.get().open(file_location) method to open any file in a web browser. The webbrowser module in python provides a high-level interface to control the web browser.

The following code with help you open any file in the web browser. You have to pass the location of the file to the function and the rest will be done by the open_in_browser(file_location) function.

See the Following Python 3 Code Example:
import webbrowser
import  os

def open_in_browser(file_location):
    """Attempt to open file located at file_location in the default web

    # If just the name of the file was given, check if it's in the Current
    # Working Directory.
    if not os.path.isfile(file_location):
        file_location = os.path.join(os.getcwd(), file_location)
    if not os.path.isfile(file_location):
        raise IOError("\n\nFile not found.")

    new = 2  # open in a new tab, if possible
    webbrowser.get().open(file_location, new=new) 
Summary and Conclusion:-

The Python webbrowser module will help you open the web browser and you have seen what we can do with the different methods available in the package. Read more about the webbrowser module from the official docs.

You Might Also Like to Read:
how to open browser

Download Free

Post a Comment

* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !