尝试启动 Firefox 时出现 Python selenium 错误 [英] Python selenium error when trying to launch firefox

查看:25
本文介绍了尝试启动 Firefox 时出现 Python selenium 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我输入的代码如下:

from selenium import webdriver驱动程序 = webdriver.Firefox()

代码返回的错误如下:

 WindowsError Traceback(最近一次调用)<ipython-input-7-fd567e24185f>在 <module>()---->1 个驱动程序 = webdriver.Firefox()C:Anacondalibsite-packagesseleniumwebdriverfirefoxwebdriver.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,超时),59 required_capabilities=能力)60 self._is_remote = FalseC:Anacondalibsite-packagesseleniumwebdriverfirefoxextension_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 远程连接.__init__(C:Anacondalibsite-packagesseleniumwebdriverfirefoxfirefox_binary.pyc 在launch_browser(self, profile)45 self.profile = 个人资料46--->47 self._start_from_profile_path(self.profile.path)48 self._wait_until_connectable()49C:Anacondalibsite-packagesseleniumwebdriverfirefoxfirefox_binary.pyc in _start_from_profile_path(self, path)7172 Popen(命令,标准输出=管道,标准错误=标准输出,--->73 env=self._firefox_env).communicate()74 命令[1] = '-前景'75 self.process = Popen(C:Anacondalibsubprocess.pyc in __init__(self、args、bufsize、可执行文件、stdin、stdout、stderr、preexec_fn、close_fds、shell、cwd、env、universal_newlines、startupinfo、creationflags)第677话第678话-->第679话680第681话C:Anacondalibsubprocess.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章第 895 章-->第896话第 897 章898 # 将 pywintypes.error 转换为 WindowsError,即WindowsError: [错误 2] 系统找不到指定的文件

解决方案

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

from selenium import webdriver从 selenium.webdriver.firefox.firefox_binary 导入 FirefoxBinarybinary = FirefoxBinary('path/to/binary')驱动程序 = webdriver.Firefox(firefox_binary=binary)

FirefoxDriver 寻找的默认路径位于 %PROGRAMFILES%Mozilla Firefoxfirefox.exe.请参阅 FirefoxDriver

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

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:Anacondalibsite-packagesseleniumwebdriverfirefoxwebdriver.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:Anacondalibsite-packagesseleniumwebdriverfirefoxextension_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:Anacondalibsite-packagesseleniumwebdriverfirefoxfirefox_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:Anacondalibsite-packagesseleniumwebdriverfirefoxfirefox_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:Anacondalibsubprocess.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:Anacondalibsubprocess.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

解决方案

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 Firefoxfirefox.exe. See FirefoxDriver

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

这篇关于尝试启动 Firefox 时出现 Python selenium 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆