本文介绍了NotADirectoryError:[WinError 267]通过Selenium Python调用Firefox时,目录名称无效错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从python代码下面使用Selenium Webdriver调用firefox浏览器.

I'm trying to invoke a firefox browser using Selenium webdriver from below python code..

from selenium import webdriver

# Initializing the WebDriver for Firefox browser
driver = webdriver.Firefox("C:\\selenium\\mozilla\\geckodriver.exe")
driver.set_page_load_timeout(30)
driver.maximize_window()
driver.get("https://www.google.com/")

# Closing the reference
driver.quit()

,但它总是会引发如下错误,但这适用于Chrome浏览器.

but it is always throwing an error like below, however this is working for Chrome browser.

Traceback (most recent call last):
  File "C:/Python/Practice/FirefoxSample.py", line 8, in <module>
    driver = webdriver.Firefox("C:\\selenium\\mozilla\\geckodriver.exe")
  File "C:\Python\venv\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 139, in __init__
    firefox_profile = FirefoxProfile(firefox_profile)
  File "C:\Python\venv\lib\site-packages\selenium\webdriver\firefox\firefox_profile.py", line 78, in __init__
    ignore=shutil.ignore_patterns("parent.lock", "lock", ".parentlock"))
  File "C:\Python\Python36-32\lib\shutil.py", line 309, in copytree
    names = os.listdir(src)
NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\\selenium\\mozilla\\geckodriver.exe'

Process finished with exit code 1

我在这里想念什么?

我也尝试过使用pip升级硒包

I have also tried upgrading the selenium package using the pip

pip install -U selenium

其他信息:运行Firefox最新版本(59.0.2),Python(3.6.5)和Selenium Gecko Webdriver(0.20.0).不知道是否需要任何帮助.

Additional info: running the Firefox latest version (59.0.2), Python (3.6.5) and Selenium Gecko webdriver (0.20.0). Not sure if anything is needed to help on this.

推荐答案

您需要注意以下几点:

  • 您需要传递 Key executable_path 以及 Value 来引用 GeckoDriver 通过单个反斜杠(即\)以及原始的r开关,如下所示:

  • You need to pass the Key executable_path along with the Value referring to the absolute path of the GeckoDriver through single backslash i.e. \ along with the raw i.e. r switch as follows :

from selenium import webdriver

driver = webdriver.Firefox(executable_path=r'C:\selenium\mozilla\geckodriver.exe')
driver.set_page_load_timeout(30)
driver.get("https://www.google.com/")
driver.quit()

通过您的 IDE

  • 清洁您的项目工作区重建您的项目,并且仅具有必需的依赖项.

  • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.

    这篇关于NotADirectoryError:[WinError 267]通过Selenium Python调用Firefox时,目录名称无效错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 10-22 23:10