Python selenium error when trying to launch firefox
Asked Answered
C

9

18

I am getting an error when trying to open Firefox using Selenium in ipython notebook. I've looked around and have found similar errors but nothing that exactly matches the error I'm getting. Anybody know what the problem might be and how I fix it? I'm using Firefox 22.

The code I typed in was as follows:

from selenium import webdriver
driver = webdriver.Firefox()

The error the code returns is as follows:

    WindowsError                              Traceback (most recent call last)
<ipython-input-7-fd567e24185f> in <module>()
----> 1 driver = webdriver.Firefox()

C:\Anaconda\lib\site-packages\selenium\webdriver\firefox\webdriver.pyc in __init__(self, firefox_profile, firefox_binary, timeout, capabilities, proxy)
     56         RemoteWebDriver.__init__(self,
     57             command_executor=ExtensionConnection("127.0.0.1", self.profile,
---> 58             self.binary, timeout),
     59             desired_capabilities=capabilities)
     60         self._is_remote = False

C:\Anaconda\lib\site-packages\selenium\webdriver\firefox\extension_connection.pyc in __init__(self, host, firefox_profile, firefox_binary, timeout)
     45         self.profile.add_extension()
     46 
---> 47         self.binary.launch_browser(self.profile)
     48         _URL = "http://%s:%d/hub" % (HOST, PORT)
     49         RemoteConnection.__init__(

C:\Anaconda\lib\site-packages\selenium\webdriver\firefox\firefox_binary.pyc in launch_browser(self, profile)
     45         self.profile = profile
     46 
---> 47         self._start_from_profile_path(self.profile.path)
     48         self._wait_until_connectable()
     49 

C:\Anaconda\lib\site-packages\selenium\webdriver\firefox\firefox_binary.pyc in _start_from_profile_path(self, path)
     71 
     72         Popen(command, stdout=PIPE, stderr=STDOUT,
---> 73               env=self._firefox_env).communicate()
     74         command[1] = '-foreground'
     75         self.process = Popen(

C:\Anaconda\lib\subprocess.pyc in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags)
    677                             p2cread, p2cwrite,
    678                             c2pread, c2pwrite,
--> 679                             errread, errwrite)
    680 
    681         if mswindows:

C:\Anaconda\lib\subprocess.pyc in _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
    894                                          env,
    895                                          cwd,
--> 896                                          startupinfo)
    897             except pywintypes.error, e:
    898                 # Translate pywintypes.error to WindowsError, which is

WindowsError: [Error 2] The system cannot find the file specified
Chamois answered 10/7, 2013 at 20:57 Comment(2)
The same code works for me. I'm using Firefox 22 and Python 2.74. Is Firefox in your PATH?Rhiamon
user1177636, this is the first time I've tried to run selenium so I haven't tried to run it with a previous version of Firefox. Tobi, path for Firefox is C:/Users/myname/appdata/Local/Mozilla Firefox/ - is that considered "in my path"?Chamois
T
28

Try specify your Firefox binary when initialize Firefox()

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

binary = FirefoxBinary('path/to/binary')
driver = webdriver.Firefox(firefox_binary=binary)

The default path FirefoxDriver looking for is at %PROGRAMFILES%\Mozilla Firefox\firefox.exe. See FirefoxDriver

Or add your path of Firefox binary to Windows' PATH.

Thedrick answered 10/7, 2013 at 22:24 Comment(2)
User1177636, thanks for the advice. I tried what you suggested using the following code: from selenium.webdriver.firefox.firefox_binary import FirefoxBinary binary = FirefoxBinary('Appdata/Local/Mozilla Firefox/') driver = webdriver.Firefox(firefox_binary=binary) but I got the same error. I tried the path with an / prior to Appdata with the same result. Is the path I'm typing wrong?Chamois
No, try something like FirefoxBinary('C:/Users/myname/appdata/Local/Mozilla Firefox/firefox.exe') first. Make you the path is correct. Open Windows Explorer and see if it's actually there.Thedrick
C
2

Problem is ocuring because you don't have geckodriver

Solution:

  1. Go to this website and download appropriate version for you machine, make sure that you have .exe file inside the archieve.
  2. Then unpack and copy .exe file to your directory
Celestyna answered 25/4, 2020 at 13:50 Comment(0)
C
1

Requirements:

  • Ubuntu 16.04 (i386)
  • Firefox 65.0.1.
  • python 3.8.5
  • geckodriver-v0.27.0-linux32.tar.gz

This is what I do:

  • pip3 install selenium

  • Download geckodriver v0.27.0

  • Extract geckodriver

  • mv geckodriver /usr/local/bin/

  • export PATH=$PATH:/usr/local/bin/geckodriver

  • check Xauthority with command --> locate Xauthority

  • cd /home/your-user/Xauthority

  • chown root: .Xauthority

  • create python code:

    from selenium import webdriver

    browser = webdriver.Firefox() browser.get('http://localhost')

  • Run python script.

Contemplative answered 2/9, 2020 at 4:26 Comment(0)
A
1

A similar error happened to me on Linux. Solved it by installing firefox with apt instead of snap.

Apologetic answered 16/2, 2022 at 12:11 Comment(0)
H
0

I got the same error when I set environment variable export PYTHONDONTWRITEBYTECODE=1 to get rid of pyc files on every test run. I was able to revert the change by updating selenium pip install --upgrade selenium. OSX (10.10)

Housemaster answered 7/6, 2016 at 4:1 Comment(0)
O
0

This is what worked:

apt-get update

apt-get install -y xorg xvfb firefox dbus-x11 xfonts-100dpi xfonts-75dpi xfonts-cyrillic
Ogilvy answered 17/7, 2016 at 12:6 Comment(2)
Please mention what this commands does. It may solve problem but other users can take little help from description.Differential
Basically xvfb was failing to initialize due to additional dependencies.the second line contains all the packages required for running xvfb.. which allows firefox to run in a "headless" mode.Ogilvy
L
0
    driver=webdriver.Firefox(executable_path="add geckodriver.exe",log_path=None)
Luht answered 1/6, 2017 at 12:41 Comment(0)
I
0

You are required to install geckodriver:

https://selenium-python.readthedocs.io/installation.html

Just download it, unzip it, and copy it to your python directory... Simple.

Inflect answered 25/3, 2019 at 6:47 Comment(0)
S
-1

those 2 packages are needed (ubuntu)!

apt-get update
apt-get install -y xorg xvfb firefox dbus-x11 xfonts-100dpi xfonts-75dpi xfonts-cyrillic

sudo apt-get install build-essential curl git m4 ruby texinfo libbz2-dev libcurl4-openssl-dev libexpat-dev libncurses-dev zlib1g-dev
sudo apt install linuxbrew-wrapper
brew install geckodriver

also what working for me is to use chrome instead of firefox check this tutorial: https://christopher.su/2015/selenium-chromedriver-ubuntu/

Sisneros answered 17/5, 2017 at 22:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.