【问题标题】:FileNotFoundError with Selenium (Python 3.8) [duplicate]Selenium 的 FileNotFoundError(Python 3.8)[重复]
【发布时间】:2020-07-15 03:53:06
【问题描述】:

我查看了其他几个论坛帖子并发现了类似的问题,但在尝试了所有解决方案之后,到目前为止都没有奏效。这是我的代码(直接取自我一直在关注的教程):

from time import sleep
from selenium import webdriver
driver = webdriver.Firefox(executable_path='/opt/geckoDriver/geckodriver.exe')
driver.get('https://instagram.com/')
sleep(5)
driver.close()

这是它吐出的错误:

Traceback (most recent call last):
  File "C:\Users\14053\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "C:\Users\14053\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\14053\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 1307, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
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 "D:\Documents\Python\Instagram bot 0.py", line 5, in <module>
    driver = webdriver.Firefox(executable_path='/opt/geckoDriver/geckodriver.exe')
  File "C:\Users\14053\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 164, in __init__
    self.service.start()
  File "C:\Users\14053\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'geckodriver.exe' executable needs to be in PATH. 

我已经下载了 geckodriver 可执行文件,但它只是在我的下载文件夹中,因为我不知道将它放在哪里以便代码访问它。我尝试了几个地方,但运气不佳。

(总的来说,我对编程/索引有些陌生,所以请像我是一个不知道的学生一样向我解释)

【问题讨论】:

  • 您应该指定 geckodriver.exe 文件在可执行文件路径中的位置,而不是使用您那里的文件。

标签: python python-3.x selenium selenium-webdriver


【解决方案1】:

现在,代码使用了一个不引用任何内容的路径。这就是您收到FileNotFound 错误的原因。您应该将 executable_path 更改为 geckoDriver 可执行文件的路径,而不是 executable_path='/opt/geckoDriver/geckodriver.exe'。所以,它就像(但不是)C:\\Users\\Downloads\\geckoDriver.exe。您还应该确保反斜杠被转义,否则您将收到另一个 FileNotFound 错误,因为它会尝试转义后面的字符。

【讨论】:

    猜你喜欢
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 2020-08-26
    • 2020-12-12
    • 2020-07-03
    相关资源
    最近更新 更多