我是编程新手,参加过一些 Python 类(class)并正在尝试应用我所学的知识。
我正在运行 macOS Sierra 并在我的机器上安装了 python2 和 3,尽管我只想使用 python3,但我之前的类(class)指示我从 python2 开始,我不知道这是不是坏事。
无论如何,在使用 Python 自动化无聊的东西类(class)(使用 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]
我在这里找到了似乎可以解决我的问题的答案: Selenium using Python - Geckodriver executable needs to be in PATH
但我不太明白如何在我的计算机中操作 PATH 或如何以计算机工作的方式组织我的文件。
我在我的终端上执行了以下代码(按照其他查询中的指示): exportPATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step
但这对我来说没有任何意义,也没有用。我还尝试从下载(它最初所在的位置)中获取 Geckodriver 文件并将其放入我的 Anaconda 文件夹中。
无论如何,我很确定问题在于我真的不知道计算机是如何组织自身的,因此无法正确处理代码。
因此,我想针对我的具体情况寻求解决方案,并提供引用文本、教程、视频或任何类似的东西,以便我更好地理解这一切是如何工作的(我仍然没有'在这方面找不到任何好的 Material )。
提前致谢!
请您参考如下方法:
我发现了同样的错误,这里是步骤:
苹果机:
从以下链接下载适用于 MacOS 的 geckodriver:
转到终端并输入以下命令以了解 Python 的路径:
echo $PATH一般路径是/usr/local/bin。
将 geckodriver 从下载文件夹复制到您在第 2 步中获得的路径。使用以下命令:
cp downloads /usr/local/bin
注意:有时在执行第 3 步时,您可能会遇到permission denied 错误,要解决此错误,您应该在前面使用 sudo像这样的命令:
sudo cp downloads /usr/local/bin
之后您必须输入您的帐户密码。仅供引用,sudo 让您以管理员身份运行命令。
