NotADirectoryError: [WinError 267] The directory name is invalid error while invoking Firefox through Selenium Python
Asked Answered
S

5

16

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()

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

What am I missing here?

I have also tried upgrading the selenium package using the pip

pip install -U selenium

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.

Stapleton answered 19/4, 2018 at 20:6 Comment(3)
I have also tried upgrading the selenium package using the pipStapleton
I have also tried upgrading the selenium package using the pip pip install -U selenium Also, 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.Stapleton
maybe use github.com/SergeyPirogov/webdriver_manager instead of installing each driver manually?Marjoriemarjory
M
33

You need to take care of a couple of things as follows :

  • 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()
    
  • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.

  • Use CCleaner tool to wipe off all the OS chores before and after the execution of your Test Suite.
  • If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
  • Take a System Reboot.
  • Execute your @Test.
Mahlstick answered 19/4, 2018 at 20:52 Comment(1)
I found this while testing randomly loading chrome/edge/firefox drivers, turns out chrome & edge do not need executable_path explicitly assigned while firefox does. Leaving this comment so others can avoid having to rediscover that trap.Weaponeer
A
2

Selenium is looking for the directory, not the executable. You have to point to the directory where geckodriver.exe is placed. Do not include geckodriver.exe in the string argument.

Instead of

driver = webdriver.Firefox("C:\\selenium\\mozilla\\geckodriver.exe")

Do

driver = webdriver.Firefox("C:\\selenium\\mozilla")
Aluminum answered 2/2, 2021 at 18:38 Comment(0)
W
0

I found this Q&A while testing code to randomly load chrome/edge/firefox drivers.

Turns out chrome & edge do not need executable_path explicitly assigned while firefox does.

I'd also reccomend randomly setting the window size before opening a session to aid in meachanisms to defeat scraping. (be aware that may affect screen layout so be conservative in range of sizes).

How to set window size in Selenium Chrome Python

Weaponeer answered 19/10, 2021 at 8:54 Comment(0)
D
0

I think this should work without any errors or warnings


from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options

# provide a full path of the driver
path = r"F:\scraping\selenium\geckodriver.exe"

# pass the driver path as a service
service = Service(path)
driver = webdriver.Firefox(service=service)

driver.set_page_load_timeout(30)
driver.get("https://www.google.com/")
driver.quit()
Deuno answered 1/6, 2022 at 12:36 Comment(0)
D
0

Please follow the below steps. Then you will be able to invoke the firefox browser.

Open cmd and enter "where python"

Inside Scripts folder paste the latest geckodriver

import "from selenium.webdriver.firefox.firefox_binary import FirefoxBinary"

Delphina answered 27/1, 2023 at 10:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.