“WebDriverException:'geckodriver'可执行文件需要在PATH中”错误,而'driver = webdriver.Firefox()'通过Selenium

时间:2018-06-11 01:39:53

标签: python selenium firefox selenium-webdriver geckodriver

对于那里的专家,我使用Spyder在Mac上运行以下代码(在Mac上):

>from selenium import webdriver
>from selenium.webdriver.common.keys import Keys
>driver = webdriver.Firefox()

我收到以下错误:

>Traceback (most recent call last):
>
>  File "<ipython-input-4-fd567e24185f>", line 1, in <module>
>    driver = webdriver.Firefox()
>
>  File "/Users/liminfang/anaconda3/lib/python3.6/site->packages/selenium/webdriver/firefox/webdriver.py", line 160, in __init__
>    self.service.start()
>
>  File "/Users/liminfang/anaconda3/lib/python3.6/site->packages/selenium/webdriver/common/service.py", line 83, in start
>    os.path.basename(self.path), self.start_error_message)
>
>WebDriverException: 'geckodriver' executable needs to be in PATH. 

请注意,我已经通过执行

将“geckodriver”可执行文件添加到PATH中
>export PATH=$PATH:/Users/liminfang/anaconda3/geckodriver 
终端中的

我重新启动了计算机,但错误仍然存​​在。谁知道这里发生了什么?请帮忙。谢谢!

2 个答案:

答案 0 :(得分:2)

geckodriver.exe放入脚本文件夹,然后使用以下代码致电webdriver.Firefox

driver = webdriver.Firefox(firefox_binary=binary, firefox_profile=fp, executable_path='geckodriver.exe')

我已经52.0.2(64位)和Geckodriver 0.15.0-win64并且它正在工作!

答案 1 :(得分:0)

此错误消息......

WebDriverException: 'geckodriver' executable needs to be in PATH. 

...表示在您PATH变量中提到的目录中找不到 GeckoDriver

解决方案

当您使用 ipython 时,您可以从mozilla/geckodriver下载最新的 GeckoDriver 并将其放置在系统中的任何位置。初始化新 WebDriver / WebClient 会话时,通过 executable_path传递 GeckoDriver 二进制文件的绝对路径参数如下:

from selenium import webdriver
driver = webdriver.Firefox(executable_path='/path/to/geckodriver')