首页 > 解决方案 > Selenium 打开 geckdriver 错误:NotADirectoryError: [WinError 267] 目录名无效

问题描述

import time
import pyautogui
import sys
from selenium import webdriver

tabTimer = 0


fileRun = __file__
fileRun = fileRun.replace("app.py", "geckodriver.exe")

print(fileRun)




stall  = input("enter filepath: ")

driver = webdriver.Firefox(fileRun)
driver.get("htpps://google.com")

每当我尝试打开 geckodriver 时,无论是使用下面使用的方法还是手动将文件路径输入为 r"",它都无法正常工作并生成以下错误。任何帮助或建议表示赞赏,谢谢!

c:\Users\ellio\Desktop\main\app.py:22: DeprecationWarning: firefox_profile has been deprecated, please pass in an Options object
  driver = webdriver.Firefox(fileRun)
Traceback (most recent call last):
  File "c:\Users\ellio\Desktop\main\app.py", line 22, in <module>
    driver = webdriver.Firefox(fileRun)
  File "C:\Users\ellio\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 160, in __init__   
    firefox_profile = FirefoxProfile(firefox_profile)
  File "C:\Users\ellio\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\firefox\firefox_profile.py", line 74, in __init__
    shutil.copytree(self.profile_dir, newprof,
  File "C:\Users\ellio\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 555, in copytree
    with os.scandir(src) as itr:
NotADirectoryError: [WinError 267] The directory name is invalid: 'c:\\Users\\ellio\\Desktop\\main\\geckodriver.exe'

标签: pythonpython-3.xselenium-webdriver

解决方案


NotADirectoryError: [WinError 267]表示webriver的火狐方式是在找目录,而不是文件路径。如果您查看堆栈跟踪,您会看到错误是由os.scandir. os.scandir只接受目录,不接受直接文件路径。

如果你geckodriver.exe在路径的尽头省略它应该没问题。


推荐阅读