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 specific Firefox browser tests run using python selenium is possible and as easy as running tests in other scenarios using Selenium WebDriver.
In java, We can do it in two ways:
System.setProperty("webdriver.gecko.driver", "Path_To_Firefox_Driver/geckodriver") System.setProperty("webdriver.firefox.bin", "Path_To_Firefox_Binary") WebDriver driver = new FirefoxDriver() driver.get("http://www.google.com")
FirefoxBinary firefoxBinary = new FirefoxBinary(new File("Path_To_Firefox_Binary")) DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox(); desiredCapabilities.setCapability(FirefoxDriver.BINARY, firefoxBinary) WebDriver driver = new FirefoxDriver(desiredCapabilities) driver.get("http://www.google.com")
Let’s see how can we do the same in Python.
- import FirefoxBinary module from selenium.webdriver.firefox.firefox_binary
- Create a new FirefoxBinary object, Firefox binary path as an argument
- Pass created FirefoxBinary object while creating Firefox webdriver
from selenium import webdriver from selenium.webdriver.firefox.firefox_binary import FirefoxBinary binary = FirefoxBinary('path/to/binary') driver = webdriver.Firefox(firefox_binary=binary)
Sometime we may want to run tests in older Firefox version or different versions of Firefox browser available in the same machine.
Sometimes Firefox is not installed in default location and 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.