selenium使用Python – Geckodriver可执行文件需要在PATH中

大约两个月前,我是编程新手,并开始使用Python ,并且正在通过使用Python文本自动化烦人的东西 。 我正在使用IDLE,并已安装selenium模块和Firefox浏览器。 每当我试图运行webdriverfunction,我得到这个:

 from selenium import webdriver browser = webdriver.Firefox() 

例外: –

 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的path,但不知道如何,所以任何人都可以告诉我如何做到这一点?

selenium.common.exceptions.WebDriverException:消息:'geckodriver'可执行文件需要在PATH中。

首先,你需要从这里下载最新的可执行的geckodriver来使用selenium运行最新的Firefox

实际上,Selenium客户端绑定尝试从系统PATHfindgeckodriver可执行文件。 您将需要将包含可执行文件的目录添加到系统path。

  • 在Unix系统上,如果您使用的是兼容bash的shell程序,则可以执行以下操作将其附加到系统的searchpath中:

     export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step 
  • 在Windows上,您将需要更新Path系统variables,以 手动或命令行方式 将完整的目录path添加到可执行的geckodriver (不要忘记在将可执行geckodriver添加到系统PATH中以使生效后重新启动系统) 。 原理和Unix上一样。

现在你可以像下面一样运行你的代码了:

 from selenium import webdriver browser = webdriver.Firefox() 

selenium.common.exceptions.WebDriverException:消息:预期的浏览器二进制位置,但无法在默认位置find二进制文件,没有提供“moz:firefoxOptions.binary”function,并且在命令行上没有设置二进制标志

例外清楚地表明,你已经安装了Firefox的一些其他的位置,而selenium试图findFirefox和启动从默认位置,但它找不到。 您需要明确提供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) 

这一步解决了我在Ubuntu的Firefox 50。

  1. 下载geckodriver

  2. 在/ usr / local / bin中拷贝geckodriver

你不需要添加

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

这为我解决了。

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

@saurabh的答案解决了这个问题,但并没有解释为什么使用Python自动化枯燥的东西不包括这些步骤。

这是由于该书基于selenium2.x和该系列的Firefox驱动程序不需要壁虎驱动程序。 当开发selenium时,用于驱动浏览器的Gecko界面不可用。

在selenium2.x系列的最新版本是2.53.6(见例如这个答案 ,为更容易的版本)。

2.53.6版本的页面根本没有提到壁虎。 但从版本3.0.2开始,文档明确指出您需要安装gecko驱动程序。

如果在升级之后(或者在新系统上安装),那么在你之前(或在你的旧系统上)工作正常的软件就不能工作了,而且你很着急,把你的virtualenv版本的selenium

 pip install selenium==2.53.6 

但当然长期的开发解决scheme是用最新版本的seleniumbuild立一个新的virtualenv,安装gecko驱动程序,并testing是否一切仍按预期工作。 但是主要版本的凹凸可能会引入其他API的更改,因此您可能希望坚持使用旧selenium,直到您有足够的信心确保自己能够修复selenium和seleniumAPI之间的差异。

在Selenium / Python上发布的这些书籍以及大多数关于这个问题的评论都没有清楚地解释在Mac上设置这个问题的逻辑(所有的东西都是Windows !!!!),这真的让人伤心。 youtubes所有皮卡在“之后”你有path设置(在我的脑海里,便宜的出路!)。 所以,对于你美妙的Mac用户,请使用以下命令来编辑你的bashpath文件:

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

然后添加一个类似这样的path…. *#设置PATH为geckodriver PATH =“/ usr / bin / geckodriver:$ {path}”导出path

为Selenium firefox设置PATH

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

设置firefox驱动程序的可执行文件path

PATH =“/ Users / yournamePATH / VEnvPythonInterpreter / lib / python2.7 / site-packages / selenium / webdriver / common / service.py:$ {PATH}”export PATH *

这对我有效。 我担心Selenium Windows社区什么时候开始玩真正的游戏,并将我们的Mac用户纳入他们傲慢的俱乐部会员资格。

实际上我发现你可以用最新的geckodriver把它放在系统path中。 目前我正在使用

geckodriver-v0.12.0-win64.zip

Firefox 50.1.0

Python 3.5.2

selenium3.0.2

Windows 10

我正在运行一个VirtualEnv(我使用PyCharmpipe理,我假设它使用Pip来安装一切)

在下面的代码中,我可以使用executable_path参数(我通过在Lib \ site-packages \ selenium \ webdriver \ firefox \ webdriver.py中查看)发现了这个geckodriver的特定path。 注意我有一个怀疑,调用webdriver参数参数的顺序是重要的,这就是为什么executable_path是我的代码中的最后(第二最后一行closures到最右边)

您可能还会注意到我使用了一个自定义的Firefoxconfiguration文件来解决sec_error_unknown_issuer问题,如果您正在testing的站点有一个不可信证书,您将遇到这个问题。 请参阅如何使用Selenium禁用Firefox的不可信连接警告?

经过调查发现,木偶司机是不完整的,仍在进行中,没有设置各种能力或configuration文件选项来解散或设置证书的工作。 所以使用自定义configuration文件更容易一些。

无论如何,这是关于如何得到geckodriver工作,而不在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') 

MAC的步骤:

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

简短的方法:

1)下载并解压Geckodriver 。

2)启动驱动程序时提及path:

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

长方法:

1)下载并解压Geckodriver 。

2)打开.bash_profile 。 如果还没有创build,可以使用以下命令: touch ~/.bash_profile 。 然后打开它: open ~/.bash_profile

3)考虑到GeckoDriver文件存在于您的Downloads文件夹中,您可以将以下行添加到.bash_profile文件中:

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

由此,您将GeckoDriver的path附加到您的系统path。 这将在执行Selenium脚本时告诉系统GeckoDriver的位置。

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

source ~/.bash_profile

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

我正在使用Windows 10,这为我工作:

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

在已经安装了Homebrew的 macOS上,您可以简单地运行terminal命令

 $ brew install geckodriver 

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

Selenium在他们的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`, eg, 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 Python设置geckodriver:

它需要使用FirefoxDriver设置geckodriverpath如下代码:

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

下载geckodriver为您的合适的操作系统(从https://github.com/mozilla/geckodriver/releases ) – >提取它在您select的文件夹 – >正确设置上述path

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

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 

在Raspberry Pi上,我必须从ARM驱动程序创build,并将geckodriver和日志path设置为:

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

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

克隆这个回购https://github.com/jackton1/script_install.git

cd script_install

./installer --gecko-driver url_to_gecko_driver

最简单的方法为Windows!
我刚刚从这里下载了最新版本的geckodriver(我有win10),并在python目录C:\Users\my.name (已经在PATH中)中添加了geckodriver.exe文件。