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.
Prerequisites:
User should have knowledge of,
- module import in python
- 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 could 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 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
export PATH=$PATH:<folderpath>
Example:
export PATH=$PATH:/data/WorkArea/BrowserDrivers
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.
3 Responses
[…] Open given URL in Firefox browser […]
[…] this post, we have seen how to start Firefox. Before executing the script, we ensured that Firefox […]
[…] Open given URL in Firefox browser […]