我是编程的新手,大约2个月前开始使用 Python
并且正在使用Python文本浏览Sweigart的Automate the Boring Stuff . 我正在使用IDLE并且已经安装了selenium模块和Firefox浏览器 . 每当我尝试运行webdriver函数时,我都会这样:
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
设置路径但不确定如何,所以有人能告诉我我该怎么办?
18 回答
这个步骤在ubuntu firefox 50上为我解决了 .
下载geckodriver
在/ usr / local / bin中复制geckodriver
你不需要添加
这解决了我 .
如果您使用的是Anaconda,则只需激活虚拟环境,然后使用以下command安装 geckodriver :
@saurabh的答案解决了这个问题,但没有解释为什么Automate with Boring Stuff with Python不包括这些步骤 .
这是因为本书基于selenium 2.x而且该系列的Firefox驱动程序不需要gecko驱动程序 . 当开发硒时,用于驱动浏览器的Gecko界面不可用 .
selenium 2.x系列中的latest version是2.53.6(参见例如this answers),以便更容易地查看版本) .
2.53.6 version page根本没有提到壁虎 . 但是从版本3.0.2开始,文档explicitly states需要安装gecko驱动程序 .
如果在升级之后(或安装在新系统上),您之前(或在旧系统上)工作正常的软件不再起作用并且您很匆忙,请将硒版本固定在您的virtualenv中
但当然,开发的长期解决方案是使用最新版本的selenium设置一个新的virtualenv,安装gecko驱动程序并测试一切是否仍按预期工作 . 但主要的版本可能会引入您的书未涵盖的其他API更改,因此您可能希望坚持使用较旧的selenium,直到您有足够的信心可以自行修复selenium2和selenium3 API之间的任何差异 .
MAC的步骤:
简单的解决方案是下载GeckoDriver并将其添加到您的系统PATH中 . 您可以使用以下两种方法之一:
简短方法:
1)下载并解压缩Geckodriver .
2)启动驱动程序时提到路径:
长方法:
1)下载并解压缩Geckodriver .
2)打开
.bash_profile
. 如果尚未创建它,可以使用以下命令执行此操作:touch ~/.bash_profile
. 然后使用以下命令打开它:open ~/.bash_profile
3)考虑到您的下载文件夹中存在GeckoDriver文件,您可以将以下行添加到
.bash_profile
文件中:通过这个,您将GeckoDriver的路径附加到您的系统路径 . 这告诉系统在执行Selenium脚本时GeckoDriver的位置 .
4)保存
.bash_profile
并强制执行 . 这会立即加载值而无需重新启动 . 为此,您可以运行以下命令:source ~/.bash_profile
5)就是这样 . 你做完了!您现在可以运行Python脚本 .
令人遗憾的是,没有一本关于Selenium / Python发表的书籍和通过谷歌发表的关于这个问题的大部分评论都没有清楚地解释在Mac上设置它的路径逻辑(一切都是Windows !!!!) . 你已经获得了路径设置(在我看来,便宜的出路!) . 因此,对于精彩的Mac用户,请使用以下命令编辑bash路径文件:
然后添加类似这样的路径.... *#为geckodriver设置路径PATH =“/ usr / bin / geckodriver:$ ”export PATH
为Selenium firefox设置PATH
PATH =“〜/ Users / yourNamePATH / VEnvPythonInterpreter / lib / python2.7 / site-packages / selenium / webdriver / firefox /:$ ”export PATH
在firefox驱动程序上设置可执行文件的PATH
PATH =“/ Users / yournamePATH / VEnvPythonInterpreter / lib / python2.7 / site-packages / selenium / webdriver / common / service.py:$ ”export PATH *
这对我有用 . 我担心的是,Selenium Windows社区什么时候开始玩真实的游戏,并将Mac用户纳入他们傲慢的俱乐部会员资格 .
First of all you will need to download latest executable geckodriver from here to run latest firefox using selenium
实际上Selenium客户端绑定试图从系统
PATH
找到geckodriver
可执行文件 . 您需要将包含可执行文件的目录添加到系统路径 .现在,您可以按照以下方式运行代码: -
例外明确表明您已安装火狐一些其他位置,而Selenium试图找到Firefox并从默认位置启动,但它找不到 . 你需要提供明确的firefox安装二进制位置来启动firefox,如下所示: -
我正在使用Windows 10,这对我有用:
从here下载geckodriver . 为您正在使用的计算机下载正确的版本
解压缩刚刚下载的文件并剪切/复制其包含的".exe"文件
导航到C: . 我的是C:\ Python27 . 将geckodriver.exe文件粘贴到此文件夹中 .
重新启动开发环境 .
尝试再次运行代码,现在应该可以使用了 .
To set up geckodriver for Selenium Python:
它需要使用FirefoxDriver设置geckodriver路径,如下所示:
下载geckodriver以获得合适的操作系统(来自https://github.com/mozilla/geckodriver/releases) - >将其解压缩到您选择的文件夹中 - >如上所述正确设置路径
我在Windows 10中使用Python 3.6.2和Selenium WebDriver 3.4.3 .
Another way to set up geckodriver:
i)只需将geckodriver.exe粘贴到/ Python / Scripts /下(在我的情况下,文件夹是:C:\ Python36 \ Scripts)
ii)现在编写如下简单代码:
访问Gecko Driver从下载部分获取gecko驱动程序的URL .
克隆这个回购https://github.com/jackton1/script_install.git
cd script_install
跑
./installer --gecko-driver url_to_gecko_driver
我使用的是Windows 10和Anaconda2 . 我尝试设置系统路径变量,但没有成功 . 然后我只是将geckodriver.exe文件添加到Anaconda2 / Scripts文件夹中,现在一切正常 . 对我来说,路径是: -
C:\ Users \用户巴维亚\ Anaconda2 \脚本
Mac 10.12.1 python 2.7.10这项工作对我来说:)
我实际上发现你可以使用最新的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来安装所有东西)
在下面的代码中,我可以使用executable_path参数使用geckodriver的特定路径(我通过查看Lib \ site-packages \ selenium \ webdriver \ firefox \ webdriver.py来发现这一点) . 注意我怀疑调用webdriver时参数参数的顺序很重要,这就是为什么executable_path在我的代码中是最后一个(最后一行到最右边)
您可能还会注意到我使用自定义的firefox配置文件来解决在您测试的站点具有不受信任的证书时将遇到的sec_error_unknown_issuer问题 . 见How to disable Firefox's untrusted connection warning using Selenium?
在调查后发现,木偶驾驶员的驾驶员仍然不完整且仍在进行中,并且没有任何设置各种能力或简档选项来解雇或设置证书 . 因此,使用自定义配置文件更容易 .
无论如何这里的代码是关于我如何使geckodriver工作而不在路径中:
在已安装Homebrew的macOS上,您只需运行Terminal命令即可
因为自制软件已经扩展了
PATH
,所以不需要修改任何启动脚本 .为此线程的未来读者提供一些额外的输入/说明:
以下足以作为Windows 7,Python 3.6,selenium 3.11的分辨率:
@dsalaj在早期针对Unix的这个主题中的注释也适用于Windows;修补PATH环境 . Windows级别的变量可以避免重启Windows系统 .
(1)下载geckodriver(如前面这个帖子中所述)并将(解压缩的)geckdriver.exe放在X:\ Folder \ of \ your \ choice中
(2)Python代码示例:
注意:(1)上述代码可能需要大约10秒才能打开指定网址的Firefox浏览器 .
(2)如果出现's no server already running at the specified url or serving a page with the title containing the string ' Django':selenium.common.exceptions.WebDriverException:python控制台将显示以下错误:消息:达到错误页面:about:neterror?e = connectionFailure&u = http%3A // localhost%3A8000 /&c = UTF-8&F =定期&d =火狐%20can%E2%80%9
The easiest way for windows!
我刚从here下载了最新版本的geckodriver(我有win10),并在python目录
C:\Users\my.name
(已经在PATH中)添加了 geckodriver.exe 文件它对我有用!Selenium在他们的DESCRIPTION.rst中回答了这个问题
基本上只需下载geckodriver,解压缩并将可执行文件移动到/ usr / bin文件夹
在Raspberry Pi上,我必须使用ARM驱动程序创建并在以下位置设置geckodriver和日志路径:
sudo nano /usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py