[TestInitialize] and [TestCleanup] at the individual test level, [ClassInitialize] and [ClassCleanup] at the class level.
When I run below python selenium web driver source code to open a Firefox browser to run an automation test in MacOS, I meet an error message like below, if you too are getting the same error. You can the solution, I stated below.
1. Python Source Code.
from selenium import webdriver browser = webdriver.Firefox() browser.get('https://www.google.com')
2. Error Messages.
Traceback (most recent call last):
File "/Users/zhaosong/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/com/dev2qa/example/selenium/RunSeleniumWithDifferentWebBrowser.py", line 16, in <module>
File "/Users/zhaosong/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/com/dev2qa/example/selenium/RunSeleniumWithDifferentWebBrowser.py", line 11, in run_webdriver_firefox
browser = webdriver.Firefox()
File "/Users/zhaosong/anaconda3/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 157, in __init__
File "/Users/zhaosong/anaconda3/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 83, in start
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
3. Resolve Method.
The key error message is that Message: ‘geckodriver’ executable needs to be in PATH. After some browsing, I found a method to resolve it.
But before any solution to take effect, we should download the related geckodriver executable file from mozilla/geckodriver. Then unzip it to a local folder such as /Users/zhaosong/Documents/WorkSpace/tool.
Specify The executable_path Parameter Value When Initialize Firefox.
This is the simplest way to fix this problem. Please see below the source code. Then the Firefox browser can be started as normal. This method also takes effect when you run or debug above python code in the Eclipse PyDev plugin.
from selenium import webdriver browser = webdriver.Firefox(executable_path = '/Users/zhaosong/Documents/WorkSpace/tool/geckodriver') browser.get('https://www.google.com')
Set geckodriver Saved Folder In OS Path Environment Variable.
Open a terminal and run the env command to show the PATH environment variable value.
If the gecko driver saved folder does not include in the PATH value. Then run below shell command in the terminal.
# Go to the current user home directory.
# Edit .bash_profile file with vim editor.
# Press keyboard esc and i key to insert geckodriver saved folder to the PATH environment variable and export PATH.
export PATH="< geckodriver executable file saved folder>:$PATH"
# Press keyboard : then wq! to save and quite.
# Make the PATH environment variable value change take effect.
Now save above python source code in a file RunFirefox.py and run it in the terminal, the Firefox browser will be started as normal.
Use Brew To Install geckodriver In MacOS.
Open a terminal and run below command.
192:~ $ brew install geckodriver
==> Pouring geckodriver--0.21.0.high_sierra.bottle.tar.gz /usr/local/Cellar/geckodriver/0.21.0: 7 files, 5.4MB
2. When the command executes successfully, you can run geckodriver --version to check that geckodriver has been installed successfully. And run which geckodriver command to see the geckodriver installation folder.
192:~$ geckodriver --version
192:~$ which geckodriver