Selenium Geckodriver和ChromeDriver无法正常工作

时间:2017-12-14 21:43:05

标签: python selenium selenium-chromedriver geckodriver

使用Python 3.6绑定运行OS 10.12.6 Selenium

尽管我付出了最大努力,但我似乎无法与Selenium合作。这是我得到的错误:

Geckodriver错误:

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1344, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver'

在处理上述异常期间,发生了另一个异常:

Traceback (most recent call last):
File "/Users/christopher.gaboury/Desktop/Scripts/safariExecutive.py", line 11, in <module>
browser = webdriver.Firefox()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 148, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 81, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

chromedriver和geckodriver的错误基本相同。

我已手动设置路径到达这些位置。同样的错误。我已将驱动程序移动到路径中已有的位置。同样的错误。我删除了我下载的两个版本并通过Homebrew安装了两个驱动程序。同样的错误。我不知道下一步该做什么。

2 个答案:

答案 0 :(得分:0)

Homebrew需要链接驱动程序。一旦完成,他们就完美地工作了。

答案 1 :(得分:0)

我现在可以毫无问题地使用 chromedriver。我将 chromedriver.exe 设置在重命名为 chromedriver 的文件夹中,然后确保文件路径在括号中。然而,尽管 pathlib.Path 识别出 geckodriver.exe 文件存在,但我在通过 selenium 打开 geckodriver 时遇到了同样的问题。看看您是否可以让 chromedriver 以这种方式工作:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser=webdriver.Chrome(r'c:\path_to_chromedriver\chromedriver\chromedriver.exe')

与上面的最后一行一样,您应该确保以原始字符串的形式包含文件路径(原始字符串确保 Python 不会将反斜杠读取为转义字符)。如果您确实想出如何使用 geckodriver 解决您的问题(因为我已经尝试通过高级设置将其添加到系统路径中但它不起作用),请告诉我,但这应该可以让 Selenium 与 Chrome 一起工作。< /p>