Selenium using Python - Geckodriver executable nee

2018-12-31 02:32发布

I'm new to programming and started with Python about 2 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 not sure how, so can anyone tell me how would I do this?

18条回答
梦寄多情
2楼-- · 2018-12-31 02:53

The answer by @saurabh solves the issue, but 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 this answers, for an easier view of the versions).

The 2.53.6 version page doesn't mention gecko at all. But since version 3.0.2 the documentation explicitly states 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 selenium2 and selenium3 API yourself.

查看更多
大哥的爱人
3楼-- · 2018-12-31 02:54

Steps for MAC:

The simple solution is to download GeckoDriver and add it to your system PATH. You can use either of the two approaches:

Short Method:

1) Download and unzip Geckodriver.

2) Mention the path while initiating the driver:

driver = webdriver.Firefox(executable_path='/your/path/to/geckodriver')

Long Method:

1) Download and unzip Geckodriver.

2) 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

3) 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.

4) Save the .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

5) That's it. You are DONE!. You can run the Python script now.

查看更多
不再属于我。
4楼-- · 2018-12-31 02:55

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 youtubes 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.

查看更多
长期被迫恋爱
5楼-- · 2018-12-31 02:56

I've actually discovered you can use the latest geckodriver with out 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, 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 paramater (I discoverd 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 (2nd 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 certifcates 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')
查看更多
深知你不懂我心
6楼-- · 2018-12-31 02:58

Visit Gecko Driver get the url for the gecko driver from the Downloads section.

Clone this repo https://github.com/jackton1/script_install.git

cd script_install

Run

./installer --gecko-driver url_to_gecko_driver

查看更多
看风景的人
7楼-- · 2018-12-31 02:59

Selenium answers this question in their DESCRIPTION.rst

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 your `PATH`, 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

查看更多
登录 后发表回答