看到很多人都遇到过这样的问题,但在我所有的搜索中,我看到了很多我不理解的相互矛盾和令人困惑的信息——这有点超出了我的新手深度。
我在 PyCharm 中安装了 Selenium,并尝试运行《Automate The Boring Stuff with Python》一书中的代码:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://inventwithpython.com')
linkElem = browser.find_element_by_link_text('Read It Online')
type(linkElem)
linkElem.click() # follows the "Read It Online" link
运行它会抛出以下异常:
Traceback (most recent call last):
File "C:\Users\LB\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\Users\LB\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\Users\LB\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 990, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/LB/Desktop/PythonProjects/AutomateTheBoringStuffProjects/generalTestingFile.py", line 2, in <module>
browser = webdriver.Firefox()
File "C:\Users\LB\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 142, in __init__
self.service.start()
File "C:\Users\LB\AppData\Local\Programs\Python\Python36-32\lib\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.
我已经下载了最新的geckodriver.exe
(特别是 64 位版本,因为我使用的是 64 位操作系统,但我运行的是 32 位 Firefox,如果可以的话?),但我不确定将其放在哪里。
我已经查找了如何更改路径,但我不知道我到底应该更改什么或它应该指向哪里。 (Firefox 文件夹?Python 文件夹?)