How to Open Firefox in Selenium with Python
Python Selenium - Open Firefox
In this tutorial, you shall learn how to open Firefox browser, using Selenium, in Python language.
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
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('/tmp/selenium/index.html')
# Close the driver
driver.quit()
Summary
In this Python Selenium Tutorial, we have seen how to create a Firefox webdriver instance and open a Firefox browser window.