Python使用selenium启动Firefox时出现错误

18

在ipython notebook中尝试使用Selenium打开Firefox时出现错误。我查找了一些类似的错误,但没有完全匹配我遇到的错误。有人知道问题可能是什么以及如何解决吗?我正在使用Firefox 22。

我输入的代码如下:

from selenium import webdriver
driver = webdriver.Firefox()

代码返回的错误如下:

    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

相同的代码对我有效。我正在使用Firefox 22和Python 2.74。Firefox是否在您的路径中? - Lily Hahn
user1177636,这是我第一次尝试运行Selenium,因此我没有尝试使用以前版本的Firefox运行它。Tobi,Firefox的路径为C:/Users/myname/appdata/Local/Mozilla Firefox/ - 这被认为是“在我的路径中”吗? - CultureQuant
9个回答

28

尝试在初始化Firefox()时指定您的Firefox二进制文件。

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

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

FirefoxDriver 默认查找的路径是 %PROGRAMFILES%\Mozilla Firefox\firefox.exe。请参见 FirefoxDriver

或者将 Firefox 可执行文件的路径添加到 Windows 的 PATH 中。


User1177636,感谢您的建议。我尝试了您建议的方法,使用了以下代码:from selenium.webdriver.firefox.firefox_binary import FirefoxBinary binary = FirefoxBinary('Appdata/Local/Mozilla Firefox/') driver = webdriver.Firefox(firefox_binary=binary) 但是我仍然遇到了相同的错误。我尝试在Appdata之前加上/,但结果仍然一样。请问我输入的路径有误吗? - CultureQuant
2
不,先尝试像FirefoxBinary('C:/Users/myname/appdata/Local/Mozilla Firefox/firefox.exe')这样的方式。确保路径是正确的。打开Windows资源管理器查看它是否实际存在。 - Yi Zeng

2
问题是因为您没有安装geckodriver
解决方案:
  1. 访问此网站,下载适合您机器的版本,并确保您在压缩包内有.exe文件。
  2. 然后解压并将.exe文件复制到您的目录中。

1

要求:

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

我的工作内容:

  • pip3 install selenium

  • 下载geckodriver v0.27.0

  • 解压缩geckodriver

  • mv geckodriver /usr/local/bin/

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

  • 使用命令 --> locate Xauthority 检查Xauthority文件

  • cd /home/your-user/Xauthority

  • chown root: .Xauthority

  • 创建Python代码:

    from selenium import webdriver

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

  • 运行Python脚本。


1
我在Linux上遇到了类似的错误。通过使用apt而不是snap安装firefox来解决它。

0

以下是有效的方法:

apt-get update

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

请说明此命令的作用。它可能会解决问题,但其他用户可以从描述中获得一些帮助。 - Jay Modi
基本上,由于附加依赖项,xvfb 无法初始化。第二行包含运行 xvfb 所需的所有软件包。这允许 Firefox 在“无头”模式下运行。 - Nisar

0
    driver=webdriver.Firefox(executable_path="add geckodriver.exe",log_path=None)

0

当我设置环境变量export PYTHONDONTWRITEBYTECODE=1以消除每次测试运行时的pyc文件时,我遇到了相同的错误。通过更新selenium pip install --upgrade selenium,我能够恢复更改。OSX(10.10)


0

-1

需要这两个软件包(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

另外,对我有用的是使用Chrome而不是Firefox 请查看此教程: https://christopher.su/2015/selenium-chromedriver-ubuntu/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接