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 03:00

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.

查看更多
浅入江南
3楼-- · 2018-12-31 03:00

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"):
查看更多
残风、尘缘若梦
4楼-- · 2018-12-31 03:02

To set up geckodriver for Selenium Python:

It needs to set geckodriver path with FirefoxDriver as 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 in 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()
查看更多
伤终究还是伤i
5楼-- · 2018-12-31 03:03

Some additional input/clarification for future readers of this thread:

The following suffices as a resolution for Windows 7, Python 3.6, selenium 3.11:

@dsalaj's note in this thread earlier for Unix is applicable to Windows as well; tinkering with the PATH env. 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

查看更多
君临天下
6楼-- · 2018-12-31 03:06

This solved it for me.

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
driver.get('http://inventwithpython.com')
查看更多
情到深处是孤独
7楼-- · 2018-12-31 03:10

The easiest way for windows!
I just downloaded the latest version geckodriver (I have win10) from here and added that geckodriver.exe file in python directory C:\Users\my.name (which already in PATH) It worked for me!

查看更多
登录 后发表回答