How to open URL in Firefox browser
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 could 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 in PATH.
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
Try running the program again. And we could see Firefox browser opens and URL given is loaded.
Note: Drivers should be executable in Linux environments, otherwise you will get exception.
To make drivers executable use command ‘chmod +x drivername’
For more knowledge on handling Firefox browser with python script like opening specific browser version or opening browser without setting environment path values, follow below topics.