尝试启动 Firefox 时出现 Python selenium 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17580730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:35:46  来源:igfitidea点击:

Python selenium error when trying to launch firefox

pythonseleniumselenium-webdriverweb-scraping

提问by CultureQuant

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.

尝试在 ipython notebook 中使用 Selenium 打开 Firefox 时出现错误。我环顾四周,发现了类似的错误,但没有与我得到的错误完全匹配。任何人都知道问题可能是什么以及我如何解决它?我正在使用 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

采纳答案by Yi Zeng

Try specify your Firefox binary when initialize Firefox()

初始化时尝试指定您的 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)

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

FirefoxDriver 寻找的默认路径在%PROGRAMFILES%\Mozilla Firefox\firefox.exe。请参阅Firefox 驱动程序

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

或者将 Firefox 二进制文件的路径添加到 Windows 的PATH

回答by Al Kan

I got the same error when I set environment variable export PYTHONDONTWRITEBYTECODE=1to 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)

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

回答by Nisar

This is what worked:

这是有效的:

apt-get update

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

回答by Bassem Shahin

those 2 packages are needed (ubuntu)!

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

对我有用的是使用 chrome 而不是 firefox 检查本教程:https: //christopher.su/2015/selenium-chromedriver-ubuntu/

回答by Ak8511

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

回答by Gilco

You are required to install geckodriver:

您需要安装 geckodriver:

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

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

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

只需下载它,解压缩它,然后将它复制到你的 python 目录中......很简单。

回答by Joseph Dance

Problem is ocuring because you don't have geckodriver

问题是因为你没有geckodriver

Solution:

解决方案:

  1. Go to this websiteand 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
  1. 转到此网站并为您的机器下载合适的版本,确保您的存档中有 .exe 文件。
  2. 然后解压并复制 .exe 文件到您的目录