FileNotFound:[WinError 2]系统在使用selenium时找不到指定的文件“错误”

2024-09-30 08:22:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Windows pro 10,使用Firefox执行python selenium程序。 正在尝试运行:

from selenium import webdriver
wb = webdriver.Firefox()
wb.get("https://stackoverflow.com")

获取以下错误(请忽略错误消息中的web浏览器):

Traceback (most recent call last):
  File "C:\Users\dilri\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\dilri\AppData\Local\Programs\Python\Python36-  32\lib\subprocess.py", line 707, in __init__
    restore_signals, start_new_session)
  File "C:\Users\dilri\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 "H:\temp.py", line 2, in <module>
    driver = webdriver.Chrome()
  File "C:\Users\dilri\AppData\Local\Programs\Python\Python36-32\lib\site-   packages\selenium\webdriver\chrome\webdriver.py", line 62, in __init__
    self.service.start()
  File "C:\Users\dilri\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: 'chromedriver'     executable needs to be in PATH. Please see     https://sites.google.com/a/chromium.org/chromedriver/home

Tags: inpyselfliblocalseleniumlinestart
1条回答
网友
1楼 · 发布于 2024-09-30 08:22:46

正如错误消息中明确提到的,首先安装适当的浏览器驱动程序,并给出其到system variable路径的路径。 就我而言:

from selenium import webdriver
driver_path = r"G:\Python\geckodriver.exe" #since on windows use raw strings 
wb = webdriver.Firefox(executable_path=driver_path)

# to check if it's working
wb.get("https://stackoverflow.com")

请记住:驱动程序路径必须是原始字符串,因为我们正在windows上工作。 另外,请注意不要将文件名指定为selenium.py

相关问题 更多 >

    热门问题