How to open Firefox in Selenium Python?

Python Selenium – Open Firefox

In this tutorial, you shall learn how to open Firefox browser, using Selenium, in Python language.

Selenium - Open Firefox

We shall use webdriver_manager for creating a driver object for the Firefox. If you do not have webdriver_manager installed, open a terminal or command prompt, and run the following pip command.

pip install webdriver-manager
Copy

Steps to open Firefox browser using Selenium Python

Step 1

The first step in creating a Firefox webdriver instance is to import GeckoDriverManager class from the webdriver_manager.firefox module.

GeckoDriverManager class instance identifies the required webdriver package for the version of Firefox installed in your system.

The webdriver package is fetched dynamically from the predefined online repository, based on your Operating System and the version of Firefox present in your system.

Step 2

Import Service class from firefox.service module present in selenium.webdriver. This shall be used for the service parameter of webdriver.Firefox().

Step 3

Create a Firefox webdriver instance using webdriver.Firefox class.

You do not have to remember these steps. Just copy the first six lines of code from the following example, and everything should be fine to create a Firefox webdriver instance and open a Firefox browser window.

Example

In the following example, we followed the above said steps to create a Firefox webdriver instance, and open a Firefox browser window.

Python Program

from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.firefox.service import Service as FirefoxService

# Initialize Firefox driver instance
driver = webdriver.Firefox(service=FirefoxService(executable_path=GeckoDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index.html')

# Close the driver
driver.quit()
Copy

Summary

In this Python Selenium Tutorial, we have seen how to create a Firefox webdriver instance and open a Firefox browser window.

Quiz on Selenium

Q1. What is the method used to interact with web elements in Selenium Python?

Not answered

Q2. Which of the following is not a method to wait for a web element to load in Selenium Python?

Not answered

Q3. What is the method used to take a screenshot in Selenium Python?

Not answered

Q4. What is the method used to select a value from a drop-down list in Selenium Python?

Not answered

Q5. What is the method used to clear the text from an input field in Selenium Python?

Not answered
Code copied to clipboard successfully 👍