【发布时间】:2026-01-16 19:45:01
【问题描述】:
我正在使用 selenium webdriver,为此我必须配置 geckodriver,为此我使用以下代码:
from selenium import webdriver
import os
from pathlib import Path
geckodriver_path = os.getcwd()+'\\'+'geckodriver.exe'
print(geckodriver_path)
new_path = (os.path.join(str(os.getcwd()), 'geckodriver.exe'))
driver = webdriver.Firefox(new_path)
每当我在 webdriver 中存储 geckdriver 时,它都会显示以下错误:
NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\\Users\\taimoor.pasha\\PycharmProjects\\web_Automation\\geckodriver.exe'
我已经使用了所有东西,比如'joins'、'sep'、abs',这些都是在 os libraray 中使用的。有人可以在这里帮助我。 谢谢
【问题讨论】:
-
你试过把它和脚本放在同一个目录吗?这是唯一对我有用的东西。
-
另外,还不如去掉这里的外括号
(os.path.join(str(os.getcwd()), 'geckodriver.exe')) -
@Ares 我已经把它放在同一个目录上,使用 os.getcwd() 的目的是确保如果我在另一个系统或任何其他环境中使用,我不应该打扰改变路径。
-
SuperStew ,它需要在那里,因为它是文件名,我必须在字符串上给出名称。
标签: python selenium-webdriver path selenium-firefoxdriver python-os