How to open Firefox browser in selenium Python

In this article, we are going to learn how to open a Firefox browser in selenium python. To execute the test script we need to launch the firefox browser window. The web browser Mozilla Firefox uses an engine named the Gecko browser engine. The engine was created by the Mozilla foundation. GeckoDriver is what is between Selenium and the Firefox browser. It lets us control the Firefox web browser from Python code. All web browser commands go through the GeckoDriver, the GeckoDriver in turn makes your browser do what you want.

The GeckoDriver is a different executable on every operating system. On Windows it is GeckoDriver.exe, but on Mac, there are no .exe files, so it’s named differently.The GeckoDriver must match the Firefox version, otherwise, you can get incompatibility issues or have the issue that it simply doesn’t work. By the end of this article, you will be able to answer the below questions.

  • How to open/launch/invoke firefox in selenium python.
  • How to open headless firefox browser in selenium python

How to open/launch firefox in selenium python


Open Mozilla Firefox browser and type in the address bar Firefox://version/ to check browser version to download the compatible version firefox Web driver as per Browser version.

  • Download Firefoxdriver from the official website. .
  • unzip and copy geckodriver.exe to the python script directory location.
  • The path of the python script directory for Us is” C:\Users\Admin\AppData\Local \Programs \Python \Python310\Scripts”. It can be different depending on what location we have chosen while installing python.
  • Run the below code to automatically open the Firefox web browser.

from selenium import webdriver
 
 
driver = webdriver.Firefox()
driver.get('https://devenum.com')


driverEdge.quit()

2. Open headless firefox browser in selenium python


  • By using options class property specify those parameters,we need while launching the brower.In this example we have passed options.headless =True to open brower without UI.
  • Driver.get(URL) :to open a specific webpage on chrome brower
  • driver.title : To get name of brower on which we running our script.
  • driver.Quit() to close the webbrowser.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver.get("https://devenum.com")
print (driver.title)
driver.quit()

3.Setup Webdriver automatically and Launch FireFox


As we saw in the above steps we have manually downloaded the web driver zip and unzipped it and the Setup path. It is time-consuming. We have to repeat all steps when a new version of the web driver is released. We can make it easy by automatically installing the driver regardless of the new version is released by using webdriver-manager. The steps to Setup web driver manager are:

  • The first step is Install the webdriver manager by using command.
pip install webdriver-manager
  • Write this code in the python code editor to launch the MOzilla Firefox browser.

from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

driver.maximize_window()
driver.get("https://google.com")
print('driver Title:',driver.title)
print('Driver name:',driver.name)
print('Driver URL:',driver.current_url)
driver.quit()