In this post, we are going to try opening Firefox browser and then load URL of our choice.
User should have knowledge of,
- module import in python
- access imported module functions
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
Create a Firefox driver instance
driver = webdriver.Firefox();
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.
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.
/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__
File “/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/service.py”, line 81, in start
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>
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’
Instead of setting PATH environment variable, driver executable can be passed as a property ‘executable_path‘ while invoking Firefox browser instance.
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.
- import FirefoxBinary module from selenium.webdriver.firefox.firefox_binary
- Create a new FirefoxBinary object by passing Firefox binary path as an argument
- 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!!!