I'm new to programming and started with Python about two months ago and am going over Sweigart's Automate the Boring Stuff with Python text. I'm using IDLE and already installed the Selenium module and the Firefox browser.
Whenever I tried to run the webdriver function, I get this:
from selenium import webdriver
browser = webdriver.Firefox()
Exception:
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00000249C0DA1080>>
Traceback (most recent call last):
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
self.stop()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00000249C0E08128>>
Traceback (most recent call last):
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
self.stop()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Traceback (most recent call last):
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 64, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\Python\Python35\lib\subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "C:\Python\Python35\lib\subprocess.py", line 1224, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
browser = webdriver.Firefox()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 135, in __init__
self.service.start()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
I think I need to set the path for geckodriver
, but I am not sure how, so how would I do this?
This question is related to
python
selenium
firefox
selenium-firefoxdriver
geckodriver
This solved it for me.
from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
driver.get('http://inventwithpython.com')
Visit Gecko Driver and get the URL for the Gecko driver from the Downloads section.
Clone this repository: https://github.com/jackton1/script_install.git
cd script_install
Run
./installer --gecko-driver https://github.com/mozilla/geckodriver/releases/download/v0.18.0/geckodriver-v0.25.0-linux64.tar.gz
I'm using Windows 10 and this worked for me:
Some additional input/clarification for future readers of this thread:
The following suffices as a resolution for Windows 7, Python 3.6, and Selenium 3.11:
dsalaj's note for another answer for Unix is applicable to Windows as well; tinkering with the PATH environment variable at the Windows level and restart of the Windows system can be avoided.
(1) Download geckodriver (as described in this thread earlier) and place the (unzipped) geckdriver.exe at X:\Folder\of\your\choice
(2) Python code sample:
import os;
os.environ["PATH"] += os.pathsep + r'X:\Folder\of\your\choice';
from selenium import webdriver;
browser = webdriver.Firefox();
browser.get('http://localhost:8000')
assert 'Django' in browser.title
Notes: (1) It may take about 10 seconds for the above code to open up the Firefox browser for the specified URL. (2) The Python console would show the following error if there's no server already running at the specified URL or serving a page with the title containing the string 'Django': selenium.common.exceptions.WebDriverException: Message: Reached error page: about:neterror?e=connectionFailure&u=http%3A//localhost%3A8000/&c=UTF-8&f=regular&d=Firefox%20can%E2%80%9
On macOS with Homebrew already installed you can simply run the Terminal command
$ brew install geckodriver
Because homebrew already did extend the PATH
there's no need to modify any startup scripts.
It's really rather sad that none of the books published on Selenium/Python and most of the comments on this issue via Google do not clearly explain the pathing logic to set this up on Mac (everything is Windows!!!!). The YouTube videos all pickup at the "after" you've got the pathing setup (in my mind, the cheap way out!). So, for you wonderful Mac users, use the following to edit your Bash path files:
touch ~/.bash_profile; open ~/.bash_profile*
Then add a path something like this....
# Setting PATH for geckodriver
PATH=“/usr/bin/geckodriver:${PATH}”
export PATH
# Setting PATH for Selenium Firefox
PATH=“~/Users/yourNamePATH/VEnvPythonInterpreter/lib/python2.7/site-packages/selenium/webdriver/firefox/:${PATH}”
export PATH
# Setting PATH for executable on Firefox driver
PATH=“/Users/yournamePATH/VEnvPythonInterpreter/lib/python2.7/site-packages/selenium/webdriver/common/service.py:${PATH}”
export PATH*
This worked for me. My concern is when will the Selenium Windows community start playing the real game and include us Mac users into their arrogant club membership.
The answer by @saurabh solves the issue, but it doesn't explain why Automate the Boring Stuff with Python doesn't include those steps.
This is caused by the book being based on Selenium 2.x and the Firefox driver for that series does not need the Gecko driver. The Gecko interface to drive the browser was not available when Selenium was being developed.
The latest version in the Selenium 2.x series is 2.53.6 (see e.g. [these answers][2], for an easier view of the versions).
The [2.53.6 version page][3] doesn't mention Gecko at all. But since version 3.0.2 the documentation [explicitly states][4] you need to install the Gecko driver.
If after an upgrade (or install on a new system), your software that worked fine before (or on your old system) doesn't work anymore and you are in a hurry, pin the Selenium version in your virtualenv by doing
pip install selenium==2.53.6
but of course the long term solution for development is to setup a new virtualenv with the latest version of selenium, install the Gecko driver and test if everything still works as expected.
But the major version bump might introduce other API changes that are not covered by your book, so you might want to stick with the older Selenium, until you are confident enough that you can fix any discrepancies between the Selenium 2 and Selenium 3 API yourself.
[2]: https://stackoverflow.com/a/40746017/1307905) [3]: https://pypi.python.org/pypi/selenium/2.53.6 [4]: https://pypi.python.org/pypi/selenium#drivers
This steps solved it for me on Ubuntu and Firefox 50.
Download geckodriver
Copy geckodriver to folder /usr/local/bin
You do not need to add:
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = '/usr/bin/firefox'
browser = webdriver.Firefox(capabilities=firefox_capabilities)
On Windows 10 it works for me downloading the geckodriver.exe. I just had to update Firefox.
Below the code that I used:
from selenium import webdriver
driver = webdriver.Firefox(
executable_path=r'C:\Users\Usuario\Desktop\Automate the boring stuff with python exercises\Web Scraping\geckodriver.exe')
driver.get('http://inventwithpython.com')
Consider installing a containerized Firefox:
docker pull selenium/standalone-firefox
docker run --rm -d -p 5555:4444 --shm-size=2g selenium/standalone-firefox
Connect using webdriver.Remote
:
driver = webdriver.Remote('http://localhost:5555/wd/hub', DesiredCapabilities.FIREFOX)
driver.set_window_size(1280, 1024)
driver.get('https://toolbox.googleapps.com/apps/browserinfo/')
driver.save_screenshot('info.png')
On macOS v10.12.1 (Sierra) and Python 2.7.10, this works for me:
def download(url):
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
browser = webdriver.Firefox(capabilities=firefox_capabilities,
executable_path=r'/Users/Do01/Documents/crawler-env/geckodriver')
browser.get(url)
return browser.page_source
If you use a virtual environment and Windows 10 (maybe it's the same for other systems), you just need to put geckodriver.exe into the following folder in your virtual environment directory:
...\my_virtual_env_directory\Scripts\geckodriver.exe
geckodriver
), x86 or 64.I see the discussions still talk about the old way of setting up geckodriver by downloading the binary and configuring the path manually.
This can be done automatically using webdriver-manager
pip install webdriver-manager
Now the above code in the question will work simply with the below change,
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
For me it was enough just to install geckodriver
in the same environment:
$ brew install geckodriver
And the code was not changed:
from selenium import webdriver
browser = webdriver.Firefox()
There are so many solutions here, and most of them still using manual ways by downloading the package manually.
The easiest solution is actually from Navarasu.
Here is the example; and it fixes the problem quickly.
Download and install the package with pip
python -m pip install webdriver-manager
Example
wolf@linux:~$ python -m pip install webdriver-manager
Collecting webdriver-manager
Using cached https://files.pythonhosted.org/packages/9c/6c/b52517f34e907fef503cebe26c93ecdc590d0190b267d38a251a348431e8/webdriver_manager-3.2.1-py2.py3-none-any.whl
... output truncated ...
Installing collected packages: configparser, colorama, crayons, certifi, chardet, urllib3, idna, requests, webdriver-manager
Successfully installed certifi-2020.6.20 chardet-3.0.4 colorama-0.4.3 configparser-5.0.0 crayons-0.3.1 idna-2.10 requests-2.24.0 urllib3-1.25.9 webdriver-manager-3.2.1
wolf@linux:~$
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
Example
wolf@linux:~$ python
Python 3.7.5 (default, Nov 7 2019, 10:50:52)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from selenium import webdriver
>>> from webdriver_manager.firefox import GeckoDriverManager
>>>
>>> driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
[WDM] - There is no [linux64] geckodriver for browser in cache
[WDM] - Getting latest mozilla release info for v0.26.0
[WDM] - Trying to download new driver from https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-linux64.tar.gz
[WDM] - Driver has been saved in cache [/home/wolf/.wdm/drivers/geckodriver/linux64/v0.26.0]
>>>
Web browser, which is Firefox in this case will be open.
Problem solved. That's it!!!
Additional note: If you look at the log above, geckodriver
was downloaded automatically from https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-linux64.tar.gz
and saved to local directory which is at /home/wolf/.wdm/drivers/geckodriver/linux64/v0.26.0
You can also copy this binary and put it in any of your executable directory which can be get from echo $PATH
command.
E.g.,
cp /home/$(whoami)/.wdm/drivers/geckodriver/linux64/v0.26.0/geckodriver /home/$(whoami)/.local/bin/
Then, let's try the sample code in https://pypi.org/project/selenium/
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://seleniumhq.org/')
The simple solution is to download GeckoDriver and add it to your system PATH. You can use either of the two approaches:
Download and unzip Geckodriver.
Mention the path while initiating the driver:
driver = webdriver.Firefox(executable_path='/your/path/to/geckodriver')
Download and unzip Geckodriver.
Open .bash_profile
. If you haven't created it yet, you can do so using the command: touch ~/.bash_profile
. Then open it using: open ~/.bash_profile
Considering GeckoDriver file is present in your Downloads folder, you can add the following line(s) to the .bash_profile
file:
PATH="/Users/<your-name>/Downloads/geckodriver:$PATH"
export PATH
By this you are appending the path to GeckoDriver to your System PATH. This tells the system where GeckoDriver is located when executing your Selenium scripts.
.bash_profile
and force it to execute. This loads the values immediately without having to reboot. To do this you can run the following command:source ~/.bash_profile
I've actually discovered you can use the latest geckodriver without putting it in the system path. Currently I'm using
https://github.com/mozilla/geckodriver/releases/download/v0.12.0/geckodriver-v0.12.0-win64.zip
Firefox 50.1.0
Python 3.5.2
Selenium 3.0.2
Windows 10
I'm running a VirtualEnv (which I manage using PyCharm, and I assume it uses Pip to install everything).
In the following code I can use a specific path for the geckodriver using the executable_path parameter (I discovered this by having a look in Lib\site-packages\selenium\webdriver\firefox\webdriver.py ). Note I have a suspicion that the order of parameter arguments when calling the webdriver is important, which is why the executable_path is last in my code (the second to last line off to the far right).
You may also notice I use a custom Firefox profile to get around the sec_error_unknown_issuer problem that you will run into if the site you're testing has an untrusted certificate. See How to disable Firefox's untrusted connection warning using Selenium?
After investigation it was found that the Marionette driver is incomplete and still in progress, and no amount of setting various capabilities or profile options for dismissing or setting certificates was going to work. So it was just easier to use a custom profile.
Anyway, here's the code on how I got the geckodriver to work without being in the path:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
#you probably don't need the next 3 lines they don't seem to work anyway
firefox_capabilities['handleAlerts'] = True
firefox_capabilities['acceptSslCerts'] = True
firefox_capabilities['acceptInsecureCerts'] = True
# In the next line I'm using a specific Firefox profile because
# I wanted to get around the sec_error_unknown_issuer problems with the new Firefox and Marionette driver
# I create a Firefox profile where I had already made an exception for the site I'm testing
# see https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles#w_starting-the-profile-manager
ffProfilePath = 'D:\Work\PyTestFramework\FirefoxSeleniumProfile'
profile = webdriver.FirefoxProfile(profile_directory=ffProfilePath)
geckoPath = 'D:\Work\PyTestFramework\geckodriver.exe'
browser = webdriver.Firefox(firefox_profile=profile, capabilities=firefox_capabilities, executable_path=geckoPath)
browser.get('http://stackoverflow.com')
On Raspberry Pi I had to create from ARM driver and set the geckodriver and log path in:
sudo nano /usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py
def __init__(self, firefox_profile=None, firefox_binary=None,
timeout=30, capabilities=None, proxy=None,
executable_path="/PATH/gecko/geckodriver",
firefox_options=None,
log_path="/PATH/geckodriver.log"):
This error message...
FileNotFoundError: [WinError 2] The system cannot find the file specified
...implies that your program was unable to locate the specified file and while handling the exception the following exception occurred:
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
... which implies that your program was unable to locate the GeckoDriver in the process of initiating/spawnning a new Browsing Context i.e. Firefox Browser session.
You can download the latest GeckoDriver from mozilla / geckodriver, unzip/untar and store the GeckoDriver binary/executable anywhere with in your system passing the absolute path of the GeckoDriver through the key executable_path
as follows:
from selenium import webdriver
driver = webdriver.Firefox(executable_path='/path/to/geckodriver')
driver.get('http://google.com/')
In case firefox is not installed at the default location (i.e. installed at a custom location) additionally you need to pass the absolute path of firefox
binary through the attribute binary_location
as follows:
# An Windows example
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\WebDrivers\geckodriver.exe')
driver.get('http://google.com/')
from webdriverdownloader import GeckoDriverDownloader # vs ChromeDriverDownloader vs OperaChromiumDriverDownloader
gdd = GeckoDriverDownloader()
gdd.download_and_install()
#gdd.download_and_install("v0.19.0")
This will get you the path to your gekodriver.exe on Windows.
from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\\Users\\username\\\bin\\geckodriver.exe')
driver.get('https://www.amazon.com/')
You can solve this issue by using a simple command if you are on Linux
First, download (https://github.com/mozilla/geckodriver/releases) and extract the ZIP file
Open the extracted folder
Open the terminal from the folder (where the geckodriver
file is located after extraction)
Now run this simple command on your terminal to copy the geckodriver into the correct folder:
sudo cp geckodriver /usr/local/bin
To set up geckodriver for Selenium Python:
It needs to set the geckodriver path with FirefoxDriver as the below code:
self.driver = webdriver.Firefox(executable_path = 'D:\Selenium_RiponAlWasim\geckodriver-v0.18.0-win64\geckodriver.exe')
Download geckodriver for your suitable OS (from https://github.com/mozilla/geckodriver/releases) ? Extract it in a folder of your choice ? Set the path correctly as mentioned above.
I'm using Python 3.6.2 and Selenium WebDriver 3.4.3 on Windows 10.
Another way to set up geckodriver:
i) Simply paste the geckodriver.exe under /Python/Scripts/ (in my case the folder was: C:\Python36\Scripts
)
ii) Now write the simple code as below:
self.driver = webdriver.Firefox()
The easiest way for Windows!
Download the latest version of geckodriver
from here. Add the geckodriver.exe file to the Python directory (or any other directory which already in PATH
). This should solve the problem (it was tested on Windows 10).
I am using Windows 10 and Anaconda 2. I tried setting the system path variable, but it didn't work out. Then I simply added geckodriver.exe file to the Anaconda 2/Scripts folder and everything works great now.
For me the path was:
C:\Users\Bhavya\Anaconda2\Scripts
If you want to add the driver paths on Windows 10:
Right click on the "This PC" icon and select "Properties"
Click on “Advanced System Settings”
Click on “Environment Variables” at the bottom of the screen
In the “User Variables” section highlight “Path” and click “Edit”
Add the paths to your variables by clicking “New” and typing in the path for the driver you are adding and hitting enter.
Once you done entering in the path, click “OK”
Keep clicking “OK” until you have closed out all the screens
geckodriver
is not installed by default.
$ geckodriver
Command 'geckodriver' not found, but it can be installed with:
sudo apt install firefox-geckodriver
$
The following command not only installs it, but it also puts it in the executable PATH
.
sudo apt install firefox-geckodriver
The problem is solved with only a single step. I had exactly the same error as you and it was gone as soon as I installed it. Go ahead and give it a try.
$ which geckodriver
/usr/bin/geckodriver
$
$ geckodriver
1337 geckodriver INFO Listening on 127.0.0.1:4444
^C
Selenium answers this question in their DESCRIPTION.rst file:
Drivers
=======Selenium requires a driver to interface with the chosen browser. Firefox, for example, requires
geckodriver <https://github.com/mozilla/geckodriver/releases>
_, which needs to be installed before the below examples can be run. Make sure it's in yourPATH
, e. g., place it in/usr/bin
or/usr/local/bin
.
Failure to observe this step will give you an error `selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Basically just download the geckodriver, unpack it and move the executable to your /usr/bin folder.
For Windows users
Use the original code as it's:
from selenium import webdriver
browser = webdriver.Firefox()
driver.get("https://www.google.com")
Then download the driver from: mozilla/geckodriver
Place it in a fixed path (permanently)... As an example, I put it in:
C:\Python35
Then go to the environment variables of the system. In the grid of "System variables" look for the Path variable and add:
;C:\Python35\geckodriver
geckodriver, not geckodriver.exe.
To add my two cents, it is also possible to do echo PATH
(Linux) and just move geckodriver
to the folder of your liking. If a system (not virtual environment) folder is the target, the driver becomes globally accessible.
This should also work for other *nix varieties as well.
export GV=v0.29.0
wget "https://github.com/mozilla/geckodriver/releases/download/$GV/geckodriver-$GV-linux64.tar.gz"
tar xvzf geckodriver-$GV-linux64.tar.gz
chmod +x geckodriver
sudo cp geckodriver /usr/local/bin/
For Mac update to:
geckodriver-$GV-macos.tar.gz
Source: Stackoverflow.com