Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Open URL in Firefox Browser from Python Application?

  • Last Updated : 22 Nov, 2021

In this article, we’ll look at how to use a Python application to access a URL in the Firefox browser. 

To do so, we’ll use the webbrowser Python module. We don’t have to install it because it comes pre-installed. There are also a variety of browsers pre-defined in this module, and for this article, we’ll be utilizing Firefox. The webbrowser module in Python is a useful web browser controller. It has a high-level interface that allows users to view Web-based documents.

webbrowser can also be used as a command-line interface. It takes a URL as an argument and adds the following parameters as options: If feasible, -n opens the URL in a new browser window, and -t opens it in a new browser tab.

Approach:

  • We’ll start by importing the webbrowser module, which comes pre-installed and does not require additional installation.
  • We’ll take the URL that the user wants to open in Firefox and save it in a variable. If you want to open only a specific URL, you may skip this step by pre-defining the URL in a variable.
  • Now we’ll make an instance of the Mozilla  class, which is a pre-defined class inside webbrowser module, and pass the executable path for Firefox from the installation directory as a parameter. The executable path of Firefox may be obtained by right-clicking its shortcut on your desktop and selecting Properties. Under the Shortcut tab, seek for the target heading and the executable path is next to it. You may also manually search for firefox.exe in your installation directory and copy the location.
  • Now we’ll use the open() function defined within the Mozilla class to display the URL in the specified browser, or the default browser if none is specified. In our case, we have already defined it to be Firefox. Other functions, such as opening the URL in a new tab, can be found in this official documentation.

Below is the implementation:

Python3




# This code is used to open URL in firefox 
# browser
  
import webbrowser
  
# To take the URL as input from the user.
print('Enter the URL: ', end="")
link = input()
  
# Passing firefox executable path to the
# Mozilla class.
firefox = webbrowser.Mozilla("C:\\Program Files\
\Mozilla Firefox\\firefox.exe")
  
# Using open() function to display the URL.
firefox.open(link)


Output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!