Mac 上 Python 中的 Selenium - Geckodriver 可执行文件需要在 PATH 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41435983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:57:14  来源:igfitidea点击:

Selenium in Python on Mac - Geckodriver executable needs to be in PATH

pythonseleniumfirefoxselenium-webdriverselenium-chromedriver

提问by JJon.Boat

I'm new to programming and started with Python about 2 months ago and am going over Sweigart's Automate the Boring Stuff with Python text. I'm using Spyder 3 and already installed the selenium module and the Firefox browser. I used the following code in python file

我是编程新手,大约 2 个月前开始使用 Python,我正在阅读 Sweigart 用 Python 文本自动化无聊的东西。我正在使用 Spyder 3 并且已经安装了 selenium 模块和 Firefox 浏览器。我在python文件中使用了以下代码

from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://inventwithpython.com')

I get this error:

我收到此错误:

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

I've downloaded geckodriver.exe in addition to going into terminal and installing it using

除了进入终端并使用安装它外,我还下载了 geckodriver.exe

brew install geckodriver

Oddly enough, if I go into terminal and type "python" and then put the code in, it works, but not when I run the file in Spyder. Where do I need to put the geckodriver.exe file for it to work? I've tried putting it in various folders (same folder as the python file, same folder as the webdriver file, in the user bin, and so on) but I get the same error

奇怪的是,如果我进入终端并输入“python”然后输入代码,它可以工作,但当我在 Spyder 中运行该文件时就不行了。我需要将 geckodriver.exe 文件放在哪里才能使其工作?我试过把它放在不同的文件夹中(与 python 文件相同的文件夹,与 webdriver 文件相同的文件夹,在用户 bin 中,等等)但我得到了同样的错误

I've looked at similar questions but can't seem to find something that works. I've also tried with Chrome but I get the same error but with chromedriver.

我看过类似的问题,但似乎找不到有用的东西。我也尝试过使用 Chrome,但我遇到了同样的错误,但使用 chromedriver。

which geckodriver

yields /usr/local/bin/geckodriver

产量 /usr/local/bin/geckodriver

I'm also on a Mac, so file paths are a little more difficult for me than on windows.

我也在 Mac 上,所以文件路径对我来说比在 Windows 上要困难一些。

采纳答案by JJon.Boat

SOLVED: I placed the geckodriver exe in /Users/sethkillian/anaconda/bin and now it works from Spyder with no problem. Thanks for the help!

已解决:我将 geckodriver exe 放在 /Users/sethkillian/anaconda/bin 中,现在它可以从 Spyder 正常运行,没有问题。谢谢您的帮助!

回答by JJon.Boat

Download the geckodriver and put it in /usr/local/bin; then use webdriver.Firefox like this:

下载geckodriver,放到/usr/local/bin;然后像这样使用 webdriver.Firefox:

from selenium import webdriver
driver = webdriver.Firefox(executable_path = '/usr/local/bin/geckodriver')

回答by Philip

Perhaps someone can explain why the path isn't found. And I also hope this helps someone else troubleshoot their own path issues.

也许有人可以解释为什么找不到路径。我也希望这有助于其他人解决他们自己的路径问题。

You can certainly put the geckodriver executible anywhere you'd like. On my Mac, I chose ~/.local/bin since its a common place for executables to be stored that are specific to a user account. For example. the Heroku CLI is placed in ~/.local/share. This approach also eliminates the need for superuser access when adding an executable to a system location like /usr/local/bin

您当然可以将 geckodriver 可执行文件放在您想要的任何位置。在我的 Mac 上,我选择了 ~/.local/bin,因为它是存储特定于用户帐户的可执行文件的常见位置。例如。Heroku CLI 放在 ~/.local/share 中。在将可执行文件添加到 /usr/local/bin 等系统位置时,这种方法还消除了对超级用户访问的需要

I then added it to the path within my .profile with

然后我将它添加到我的 .profile 中的路径中

    EXPORT PATH=$PATH:~/.local/bin

I tested by opening a terminal and checking with:

我通过打开终端并检查:

    geckodriver --version

which worked fine.

效果很好。

But from a Python virtual environment, for some reason, the system path isn't passed?? I discovered this by adding to my selenium test script:

但是从 Python 虚拟环境中,由于某种原因,系统路径没有通过??我通过添加到我的硒测试脚本中发现了这一点:

    import sys

    for p in sys.path:
        print(p)

Which showed:

其中显示:

    /Users/philip/Devel/myproject
    /Users/philip/.virtualenvs/myproject/lib/python36.zip
    /Users/philip/.virtualenvs/myproject/lib/python3.6
    /Users/philip/.virtualenvs/myproject/lib/python3.6/lib-dynload
    /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6
    /Users/philip/.virtualenvs/myproject/lib/python3.6/site-packages

So ultimately I had to specify the path with:

所以最终我不得不指定路径:

    self.browser = webdriver.Firefox(executable_path=r'/Users/philip/.local/bin/geckodriver')

This approach works fine, but I'd still like to know why I couldn't set the path in the virtual environment.

这种方法工作正常,但我仍然想知道为什么我不能在虚拟环境中设置路径。