Handle Firefox Browser using Python Selenium WebDriver

In this post, we are going to try opening Firefox browser and then load URL of our choice.

Prerequisites:
User should have knowledge of,

  1. module import in python
  2. access imported module functions

Step 1:

Import necessary modules. The selenium.webdriver module provides all the WebDriver implementations. Currently supported WebDriver implementations are Firefox, Google Chrome, Internet Explorer and Remote WebDriver.

from selenium import webdriver

Step 2:

Create a Firefox driver instance

driver = webdriver.Firefox();

Step 3:

Load URL of our choice. WebDriver will wait until the page is loaded completely (the “onload” event has fired) before returning control to next line of script.

When there is AJAX calls on load, WebDriver may not know when page has completely loaded.

driver.get("http://www.google.com");

Final block of code:

from selenium import webdriver
driver = webdriver.Firefox();
driver.get("http://www.google.com");

Save this in a python file ‘OpenUrlInFirefoxBrowser.py’ and execute. We should see Firefox browser opens and URL given is loaded.

Oops! Error.

/data/WorkArea/python/ProjectFolder $ python OpenUrlInFirefoxBrowser.py
Traceback (most recent call last):
File “OpenUrlInFirefoxBrowser.py”, line 8, in <module>
driver = webdriver.Firefox();
File “/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py”, line 144, in __init__
self.service.start()
File “/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/service.py”, line 81, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH.

The error message says, Firefox driver path is not given i.e. geckodriver, for Firefox, executable location path in PATH environment variable.

Note: For Windows, Please follow the link to set driver executable path.

To add driver folder path to environment path, use following command in Linux
export PATH=$PATH:<driver folder path>

Example:
export PATH=$PATH:/data/WorkArea/BrowserDrivers

Try running the program again. And we could see Firefox browser opens up and URL given is loaded into browser view.

Note: Drivers should be executable in Linux environments, otherwise you will get exception.
To make drivers executable use command ‘chmod +x drivername’

Alternate Method:

Instead of setting PATH environment variable, driver executable can be passed as a property ‘executable_path‘ while invoking Firefox browser instance.

Syntax:
driver = webdriver.Firefox(executable_path=”[path to driver location]”);

from selenium import webdriver
driver = webdriver.Firefox(executable_path="/data/Drivers/geckodriver");
driver.get("http://www.google.com");

How to open URL in specific Firefox browser version?

Often times we encounter a scenario where we need to run tests on different versions of Firefox browser on the same machine. This is, running tests in specific Firefox browser version using Python selenium, possible and as easy as running tests in other scenarios using Python Selenium WebDriver.

Steps:

  1. import FirefoxBinary module from selenium.webdriver.firefox.firefox_binary
  2. Create a new FirefoxBinary object by passing Firefox binary path as an argument
  3. Pass created FirefoxBinary object while creating Firefox webdriver object
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary('path/to/binary')
driver = webdriver.Firefox(firefox_binary=binary)

Why do we need this?

1.Sometime we may want to run tests in older Firefox version or different versions of Firefox browser available in the same machine.

2.Sometimes Firefox is not installed in default location because of that Selenium throws below exception. 

selenium.common.exceptions.WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no ‘moz:firefoxOptions.binary’ capability provided, and no binary flag set on the command line.

Exception clearly states that Firefox is installed in some other location while Selenium WebDriver is trying to find Firefox and launch from default location but it couldn’t find. In this case, we specify Firefox binary file location to launch it.

That’s all about opening Firefox browser and running tests. Please try and let us know your thoughts in comments. Happy scripting!!!

Leave a Reply

avatar
  Subscribe  
Notify of