查看: 2237|回复: 6

WindowsError: [Error 2] The system cannot find the file specified

[复制链接]

10

主题

72

帖子

180

积分

注册会员

Rank: 2

积分
180
发表于 2018-9-26 10:14:13 | 显示全部楼层 |阅读模式
当我在ipython笔记本中使用Selenium打开Firefox时出错。错误完全匹配。有人知道问题是什么吗?我是怎么解决的?我用的是火狐22。我输入的代码如下:
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
回复

使用道具 举报

13

主题

53

帖子

131

积分

注册会员

Rank: 2

积分
131
发表于 2018-9-26 10:19:47 | 显示全部楼层
尝试在初始化Firefox()时指定Firefox二进制文件
  1. from selenium import webdriver
  2. from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

  3. binary = FirefoxBinary('path/to/binary')
  4. driver = webdriver.Firefox(firefox_binary=binary)
复制代码

默认的路径FirefoxDriver在%PROGRAMFILES%\Mozilla Firefox\firefox.exe中查找。查看firefoxdriver或将Firefox二进制文件的路径添加到Windows路径。
回复

使用道具 举报

5

主题

41

帖子

108

积分

注册会员

Rank: 2

积分
108
发表于 2018-9-26 10:20:32 | 显示全部楼层
当我设置环境变量export PYTHONDONTWRITEBYTECODE=1以在每次测试运行时删除pyc文件时,我得到了同样的错误。我可以通过更新selenium pip安装—升级selenium来恢复更改。OSX(10.10)
回复

使用道具 举报

4

主题

33

帖子

88

积分

注册会员

Rank: 2

积分
88
发表于 2018-9-26 10:21:32 | 显示全部楼层
一种解决方法
apt-get updateap -get install -y xorg xvfb firefox dbus-x11 xfonts-100dpi xfonts-75dpi xfonts-cyrillic
回复

使用道具 举报

11

主题

80

帖子

199

积分

注册会员

Rank: 2

积分
199
发表于 2018-9-26 10:23:16 | 显示全部楼层
这两个包是必需的(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而不是火狐更有效的方法是查看本教程
https://christopher.su/2015/selenium-chromedriver-ubuntu/
回复

使用道具 举报

4

主题

37

帖子

98

积分

注册会员

Rank: 2

积分
98
发表于 2018-9-26 10:24:12 | 显示全部楼层
driver=webdriver.Firefox(executable_path="add geckodriver.exe",log_path=None)
回复

使用道具 举报

1

主题

49

帖子

118

积分

注册会员

Rank: 2

积分
118
发表于 2018-10-9 08:11:44 | 显示全部楼层
可以的,挺不错
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表