使用Python的Selenium-Geckodriver可执行文件必须位于PATH中

问题:使用Python的Selenium-Geckodriver可执行文件必须位于PATH中

我是编程的新手,Python大约2个月前开始学习,并且正在研究Sweigart的《用Python文本自动生成无聊的东西》。我正在使用IDLE,并且已经安装了硒模块和Firefox浏览器。每当我尝试运行webdriver函数时,都会得到以下信息:

from selenium import webdriver
browser = webdriver.Firefox()

exceptions:-

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. 

我想我需要设置路径,geckodriver但不确定如何设置,所以谁能告诉我该怎么做?

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?


回答 0

selenium.common.exceptions.WebDriverException:消息:“ geckodriver”可执行文件必须位于PATH中。

首先,您需要从此处下载最新的可执行geckodriver,以使用硒运行最新的Firefox。

实际上,Selenium客户端绑定试图geckodriver从系统中找到可执行文件PATH。您需要将包含可执行文件的目录添加到系统路径。

  • 在Unix系统上,如果使用的是与bash兼容的shell,则可以执行以下操作将其附加到系统的搜索路径中:

    export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step
  • 在Windows上,您将需要更新Path系统变量以 手动命令行将完整目录路径添加到可执行geckodriver (不要忘记在将可执行geckodriver添加到系统PATH中生效后重新启动系统)。其原理与Unix相同。

现在,您可以按照以下步骤运行代码:-

from selenium import webdriver

browser = webdriver.Firefox()

selenium.common.exceptions.WebDriverException:消息:预期的浏览器二进制位置,但无法在默认位置找到二进制位置,未提供’moz:firefoxOptions.binary’功能,并且命令行上未设置二进制标志

异常清楚地表明您在Selenium试图查找Firefox并从默认位置启动时在其他位置安装了Firefox,但找不到。您需要提供明确安装了firefox的二进制位置才能启动firefox,如下所示:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary('path/to/installed firefox binary')
browser = webdriver.Firefox(firefox_binary=binary)

selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH.

First of all you will need to download latest executable geckodriver from here to run latest firefox using selenium

Actually The Selenium client bindings tries to locate the geckodriver executable from the system PATH. You will need to add the directory containing the executable to the system path.

  • On Unix systems you can do the following to append it to your system’s search path, if you’re using a bash-compatible shell:

    export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step
    
  • On Windows you will need to update the Path system variable to add the full directory path to the executable geckodriver manually or command line(don’t forget to restart your system after adding executable geckodriver into system PATH to take effect). The principle is the same as on Unix.

Now you can run your code same as you’re doing as below :-

from selenium import webdriver

browser = webdriver.Firefox()

selenium.common.exceptions.WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no ‘moz:firefoxOptions.binary’ capability provided, and no binary flag set on the command line

Exception clearly states you have installed firefox some other location while Selenium is trying to find firefox and launch from default location but it couldn’t find. You need to provide explicitly firefox installed binary location to launch firefox as below :-

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary('path/to/installed firefox binary')
browser = webdriver.Firefox(firefox_binary=binary)

回答 1

这为我解决了。

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
driver.get('http://inventwithpython.com')

This solved it for me.

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
driver.get('http://inventwithpython.com')

回答 2

这个步骤在ubuntu firefox 50上为我解决了。

  1. 下载geckodriver

  2. 将geckodriver复制到/ usr / local / bin

您不需要添加

firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = '/usr/bin/firefox'
browser = webdriver.Firefox(capabilities=firefox_capabilities)

this steps SOLVED for me on ubuntu firefox 50.

  1. Download geckodriver

  2. Copy geckodriver in /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)

回答 3

@saurabh的回答解决了这个问题,但没有解释为什么使用Python自动完成无聊的工作不包括这些步骤。

这是由于该书基于selenium 2.x,并且该系列的Firefox驱动程序不需要gecko驱动程序。开发硒时,用于驱动浏览器的Gecko界面不可用。

selenium 2.x系列的最新版本是2.53.6(请参见例如此答案,以更轻松地查看版本)。

2.53.6版本页面完全不提壁虎。但是从3.0.2版开始,文档明确指出您需要安装gecko驱动程序。

如果升级(或在新系统上安装)后,以前(或在旧系统上)运行良好的软件不再起作用,而您又急着,请执行以下操作,将硒版本固定在virtualenv中

pip install selenium==2.53.6

但是,当然,开发的长期解决方案是使用最新版本的Selenium设置新的virtualenv,安装gecko驱动程序并测试一切是否仍按预期进行。但是主要版本颠簸可能会引入书中未涵盖的其他API更改,因此您可能要坚持使用较旧的硒,直到您有足够的信心自己可以解决selenium2和selenium3 API之间的任何差异。

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.


回答 4

在已安装Homebrew的 macOS上,您只需运行Terminal命令即可

$ brew install geckodriver

因为自制软件已经扩展了,PATH所以不需要修改任何启动脚本。

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.


回答 5

为Selenium Python设置geckodriver:

它需要使用FirefoxDriver设置geckodriver路径,如下代码:

self.driver = webdriver.Firefox(executable_path = 'D:\Selenium_RiponAlWasim\geckodriver-v0.18.0-win64\geckodriver.exe')

下载适用于您的操作系统的geckodriver(从https://github.com/mozilla/geckodriver/releases)->将其提取到您选择的文件夹中->如上所述正确设置路径

我在Windows 10中使用Python 3.6.2和Selenium WebDriver 3.4.3。

设置geckodriver的另一种方法:

i)只需将geckodriver.exe粘贴在/ Python / Scripts /下(在我的情况下,文件夹为:C:\ Python36 \ Scripts)
ii)现在编写如下的简单代码:

self.driver = webdriver.Firefox()

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

回答 6

如果您使用的是Anaconda,则只需激活虚拟环境,然后使用以下命令安装geckodriver

    conda install -c conda-forge geckodriver

If you are using Anaconda, all you have to do is activate your virtual environment and then install geckodriver using the following command:

    conda install -c conda-forge geckodriver

回答 7

Ubuntu 18.04+和最新版本的geckodriver

这也应适用于其他* nix品种。

export GV=v0.26.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/

对于Mac,请更新至:

geckodriver-$GV-macos.tar.gz

Ubuntu 18.04+ and Newest release of geckodriver

This should also work for other *nix varieties as well.

export GV=v0.26.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

回答 8

我看到讨论仍在讨论通过下载二进制文件并手动配置路径来设置geckodriver的旧方法。

可以使用webdriver-manager自动完成

pip install webdriver-manager

现在,问题中的上述代码将可以简单地与以下更改一起使用,

from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

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 below change,

from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

回答 9

Windows的最简单方法!此处
下载最新版本。将geckodriver.exe文件添加到python目录(或已存在的任何其他目录)中。这应该可以解决问题(在Windows 10上测试)geckodriverPATH

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 (Tested on Windows 10)


回答 10

MAC的步骤:

简单的解决方案是下载GeckoDriver并将其添加到您的系统PATH中。您可以使用以下两种方法之一:

简短方法:

1)下载并解压缩Geckodriver

2)在启动驱动程序时提及路径:

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

长方法:

1)下载并解压缩Geckodriver

2)打开.bash_profile。如果尚未创建,则可以使用命令:touch ~/.bash_profile。然后使用以下命令打开它:open ~/.bash_profile

3)考虑到GeckoDriver文件存在于“下载”文件夹中,可以将以下行添加到该.bash_profile文件中:

PATH="/Users/<your-name>/Downloads/geckodriver:$PATH"
export PATH

这样,您会将GeckoDriver的路径附加到系统路径。这告诉系统执行Selenium脚本时GeckoDriver的位置。

4)保存.bash_profile并强制执行。这将立即加载值,而无需重新启动。为此,您可以运行以下命令:

source ~/.bash_profile

5)就这样。你做完了!您现在可以运行Python脚本。

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.


回答 11

为该线程的将来读者提供一些其他输入/说明:

以下是Windows 7,Python 3.6,Selenium 3.11的分辨率:

早先针对Unix的@dsalaj注释也适用于Windows;修改PATH环境。可以避免Windows级别的变量和Windows系统重启。

(1)下载geckodriver(如本主题前面所述),然后将(未压缩的)geckdriver.exe放在X:\ Folder \ of \ your \ choice中

(2)Python代码示例:

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

注意:(1)上面的代码可能需要大约10秒钟才能为指定的URL打开Firefox浏览器。
(2)如果没有服务器已经在指定的url上运行,或者没有提供标题为字符串’Django’的页面,则python控制台将显示以下错误:selenium.common.exceptions.WebDriverException:消息:已到达错误页面:关于:neterror?e = connectionFailure&u = http%3A // localhost%3A8000 /&c = UTF-8&f = regular&d = Firefox%20can%E2%80%9

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


回答 12

我实际上发现您可以使用最新的geckodriver,而无需将其放入系统路径中。目前我正在使用

https://github.com/mozilla/geckodriver/releases/download/v0.12.0/geckodriver-v0.12.0-win64.zip

Firefox 50.1.0

Python 3.5.2

硒3.0.2

Windows 10

我正在运行VirtualEnv(我使用PyCharm进行管理,假设它使用Pip来安装所有内容)

在以下代码中,我可以使用execute_path参数为geckodriver使用特定路径(我通过查看Lib \ site-packages \ selenium \ webdriver \ firefox \ webdriver.py发现了这一点)。请注意,我怀疑调用webdriver时参数参数的顺序很重要,这就是为什么execute_path在我的代码中位于最后(最右边的第二行)

您可能还会注意到,我使用自定义的firefox配置文件来解决sec_error_unknown_issuer问题,如果所测试的站点具有不受信任的证书,则会遇到该问题。请参阅如何使用Selenium禁用Firefox的不受信任的连接警告?

经调查后发现,木偶驱动程序不完整且仍在运行中,没有任何设置各种功能或配置文件选项以消除或设置证书的方法。因此,使用自定义配置文件更加容易。

无论如何,这是有关如何使geckodriver在不经路径的情况下工作的代码:

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

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

回答 13

我正在使用Windows 10,这对我有用:

  1. 此处下载geckodriver 。为您使用的计算机下载正确的版本
  2. 解压缩刚刚下载的文件,并剪切/复制其中包含的“ .exe”文件
  3. 导航至C:{您的python根文件夹}。我的是C:\ Python27。将geckodriver.exe文件粘贴到此文件夹中。
  4. 重新启动您的开发环境。
  5. 再次尝试运行代码,它现在应该可以工作了。

I’m using Windows 10 and this worked for me:

  1. Download geckodriver from here . Download the right version for the computer you are using
  2. Unzip the file you just downloaded and cut/copy the “.exe” file it contains
  3. Navigate to C:{your python root folder}. Mine was C:\Python27. Paste the geckodriver.exe file in this folder.
  4. Restart your development environment.
  5. Try running the code again, it should work now.

回答 14

考虑安装容器化的Firefox:

docker pull selenium/standalone-firefox
docker run --rm -d -p 5555:4444 --shm-size=2g selenium/standalone-firefox

使用连接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')

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

回答 15

遗憾的是,在Selenium / Python上出版的所有书籍以及通过Google对此问题的大多数评论都没有清楚地说明在Mac上进行设置的路径逻辑(一切都是Windows !!!!)。youtube使用者会在“之后”设置好路径设置(在我看来,便宜的出路!)。因此,对于您的Mac用户来说,请使用以下命令编辑bash路径文件:

> $ touch〜/ .bash_profile; 打开〜/ .bash_profile

然后添加类似以下的路径。…*#为geckodriver设置PATH PATH =“ / usr / bin / geckodriver:$ {PATH}” export PATH

为Selenium firefox设置PATH

PATH =“〜/ Users / yourNamePATH / VEnvPythonInterpreter / lib / python2.7 / site-packages / selenium / webdriver / firefox /:$ {PATH}”导出路径

在Firefox驱动程序上设置可执行文件的PATH

PATH =“ /用户/您的PATH / VEnvPythonInterpreter / lib / python2.7 / site-packages / selenium / webdriver / common / service.py:$ {PATH}”导出PATH *

这对我有用。我担心的是Selenium Windows社区何时才能开始玩真正的游戏,并让我们Mac用户加入其自负的俱乐部会员资格。

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.


回答 16

硒在他们的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.

基本上,只需下载geckodriver,将其解压缩并将可执行文件移至您的/ usr / bin文件夹

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


回答 17

对于Windows用户

使用原始代码:

from selenium import webdriver
browser = webdriver.Firefox()
driver.get("https://www.google.com")

然后从以下位置下载驱动程序:mozilla / geckodriver

(永久地)将其放置在固定路径中。例如,我将其放置在:

C:\ Python35

然后转到系统的环境变量,在“系统变量”的网格中查找Path变量并添加:

; C:\ Python35 \ geckodriver

geckodriver,而不是geckodriver.exe

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 Path variable and add:

;C:\Python35\geckodriver

geckodriver, not geckodriver.exe.


回答 18

在Raspberry Pi上,我必须从ARM驱动程序创建并在以下位置设置geckodriver和日志路径:

须藤纳米/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"):

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"):

回答 19

如果使用虚拟环境和win10(可能是其他系统的环境),则只需将geckodriver.exe放入虚拟环境目录中的以下文件夹中:

… \ my_virtual_env_directory \ Scripts \ geckodriver.exe

If you use virtual environment and win10(maybe it’s the 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


回答 20

from webdriverdownloader import GeckoDriverDownloader # vs ChromeDriverDownloader vs OperaChromiumDriverDownloader
gdd = GeckoDriverDownloader()
gdd.download_and_install()
#gdd.download_and_install("v0.19.0")

这将为您提供Windows上gekodriver.exe的路径

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\\Users\\username\\\bin\\geckodriver.exe')
driver.get('https://www.amazon.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/')

回答 21

Mac 10.12.1 python 2.7.10对我有用:)

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

Mac 10.12.1 python 2.7.10 this work 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

回答 22

我正在使用Windows 10和Anaconda2。我尝试设置系统路径变量,但没有解决。然后,我只是将geckodriver.exe文件添加到Anaconda2 / Scripts文件夹中,现在一切正常。对我来说,道路是:

C:\ Users \ Bhavya \ Anaconda2 \ Scripts

I am using Windows 10 and Anaconda2. I tried setting system path variable but didn’t worked out. Then I simply added geckodriver.exe file to Anaconda2/Scripts folder and everything works great now. For me the path was:-

C:\Users\Bhavya\Anaconda2\Scripts


回答 23

如果要在Windows 10上添加驱动程序路径:

  1. 右键单击“此PC”图标,然后选择“属性”

  2. 点击“高级系统设置”

  3. 点击屏幕底部的“环境变量”
  4. 在“用户变量”部分中,突出显示“路径”,然后单击“编辑”
  5. 通过单击“新建”并输入要添加的驱动程序的路径,然后按Enter键,将路径添加到变量中。
  6. 输入路径后,点击“确定”
  7. 持续单击“确定”,直到关闭所有屏幕

If you want to add the driver paths on windows 10:

  1. Right click on the “This PC” icon and select “Properties”

  2. Click on “Advanced System Settings”

  3. Click on “Environment Variables” at the bottom of the screen
  4. In the “User Variables” section highlight “Path” and click “Edit”
  5. Add the paths to your variables by clicking “New” and typing in the path for the driver you are adding and hitting enter.
  6. Once you done entering in the path, click “OK”
  7. Keep clicking “OK” until you have closed out all the screens

回答 24

访问Gecko驱动程序,从下载部分获取gecko驱动程序的URL。

克隆此仓库https://github.com/jackton1/script_install.git

cd script_install

./installer --gecko-driver https://github.com/mozilla/geckodriver/releases/download/v0.18.0/geckodriver-v0.25.0-linux64.tar.gz

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 https://github.com/mozilla/geckodriver/releases/download/v0.18.0/geckodriver-v0.25.0-linux64.tar.gz

回答 25

  1. 确保您具有正确版本的驱动程序(geckodriver),x86或64。
  2. 确保您正在检查正确的环境,例如,作业在Docker中运行,而检查environmnet是主机操作系统
  1. ensure you have the correct version of driver (geckodriver), x86 or 64.
  2. ensure you are checking the right environment, for example the job is running in a Docker, whereas the environmnet is checked is the host OS

回答 26

对我而言,仅在相同的环境中安装geckodriver就足够了:

$ brew install geckodriver

并且代码没有更改:

from selenium import webdriver
browser = webdriver.Firefox()

for me it was enough just to install geckodriver in the same environment:

$ brew install geckodriver

and the code was not change:

from selenium import webdriver
browser = webdriver.Firefox()

回答 27

要加上我的5美分,也可以这样做echo PATH(Linux),只需将geckodriver移到您喜欢的文件夹中即可。如果以系统(而非虚拟环境)文件夹为目标,则驱动程序变得可以全局访问。

To add my 5 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.