Selenium 和 Geckodriver 的路径问题

Path issues with Selenium and Geckodriver

我是编程新手,学过一些 python 课程,正在尝试应用我所学的知识。

我是 运行 macOS Sierra,我的机器上安装了 python2 和 3,尽管我只是想使用 python3,但我之前的课程指示我从 python2 开始,我不知道这是否是一件坏事。

无论如何,学习 Python 自动化无聊的东西课程(使用 python3)我 运行 进入此代码:

#! python3
from selenium import webdriver
browser = webdriver.Firefox()

并收到以下错误消息:

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

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Alex/Anaconda/Templates/selenium_firefox.py", line 3, in <module>
    browser = webdriver.Firefox()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 135, in __init__
    self.service.start()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/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. 

Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x1029777f0>>
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 163, in __del__
    self.stop()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 135, in stop
    if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
[Finished in 0.501s]

我在这里找到了似乎可以解决我的问题的答案:

但我不太明白如何在我的计算机中操作 PATH 或如何以计算机工作的方式组织我的文件。

我在我的终端上执行了以下代码(按照其他查询中的指示): exportPATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step

但这对我来说没有任何意义,也没有用。我还尝试从下载(最初的位置)中获取 Geckodriver 文件并将其放入我的 Anaconda 文件夹中。

无论如何,我很确定问题是我真的不知道计算机是如何组织自己的,因此无法正确处理代码。

因此,我想为我的具体案例寻求解决方案,并提供参考文本、教程、视频或任何类似的东西,以便我更好地理解这一切是如何工作的 (在这件事上我仍然没有找到任何好的 material)。

提前致谢!

您为 UNIX 找到的 link 应该可以工作。 exportPATH 之间有 space 吗?您的副本之间没有 space,所以那行不通。如果它在 UNIX 中与 Python 路径中的 geckodriver 在 Windows 中相同,那么您可以尝试:

cp geckodriver.exe \path\to\Python\

然后geckodriver存放在Python的基本路径下,从而自动初始化。

您始终可以对潜水员位置进行硬编码:

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

我发现了同样的错误,步骤如下:

Mac:

  1. 从以下 link:

    下载 MacOS 的 geckodriver

    https://github.com/mozilla/geckodriver/releases

  2. 转到终端并键入以下命令以了解 Python 的路径:

    echo $PATH
    

    一般路径会是/usr/local/bin.

  3. 将下载文件夹中的 geckodriver 复制到您在步骤 2 中获得的路径。使用以下命令:

    cp downloads /usr/local/bin 
    

注意: 有时在执行第 3 步时,您可能会遇到 permission denied 错误,要解决此错误,您应该使用 sudo 在命令前面像这样:

sudo cp downloads /usr/local/bin

之后您必须输入您的帐户密码。仅供参考,sudo 让您 运行 作为管理员执行命令。