我是编程新手,大约两个月前开始使用 Python,并且正在阅读 Sweigart 的 Automate the Boring Stuff with Python text。我正在使用 IDLE ,并且已经安装了 Selenium 模块和 Firefox 浏览器。
每当我尝试运行 webdriver 函数时,都会得到以下信息:
from selenium import webdriver
browser = webdriver.Firefox()
例外:
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00000249C0DA1080>>
Traceback (most recent call last):
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
self.stop()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00000249C0E08128>>
Traceback (most recent call last):
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
self.stop()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Traceback (most recent call last):
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 64, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\Python\Python35\lib\subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "C:\Python\Python35\lib\subprocess.py", line 1224, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
在处理上述异常的过程中,又出现了一个异常:
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
browser = webdriver.Firefox()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 135, in __init__
self.service.start()
File "C:\Python\Python35\lib\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.
我想我需要为 geckodriver
设置路径,但我不知道怎么做,那我该怎么做呢?
原文由 tadm123 发布,翻译遵循 CC BY-SA 4.0 许可协议
首先,您需要从这里下载最新的可执行 geckodriver 以使用 Selenium 运行最新的 Firefox
实际上,Selenium 客户端绑定尝试从系统
PATH
中找到geckodriver
可执行文件。您需要将包含可执行文件的目录添加到系统路径。现在您可以像下面一样运行您的代码:-
异常清楚地表明您在 Selenium 尝试查找 Firefox 并从默认位置启动时已在其他位置安装了 Firefox,但它找不到它。您需要提供明确的 Firefox 安装二进制位置来启动 Firefox,如下所示:-
https://github.com/mozilla/geckodriver/releases
对于 Windows:
从 GitHub 下载文件,解压缩,然后将其粘贴到 Python 文件中。它对我有用。
https://github.com/mozilla/geckodriver/releases
对我来说,我的路径路径是: