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
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.
Import Service class from firefox.service module present in selenium.webdriver. This shall be used for the service parameter of webdriver.Firefox().
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.
In the following example, we followed the above said steps to create a Firefox webdriver instance, and open a Firefox browser window.
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()
In this Python Selenium Tutorial, we have seen how to create a Firefox webdriver instance and open a Firefox browser window.