无法创建Selenium webdriver.Firefox()的实例

时间:2017-03-29 14:11:54

标签: python selenium python-3.6 selenium3

我在Windows 7上运行Python 3.6.0。我已经安装了Selenium 3.3.1版:

Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import selenium
>>> selenium.__version__
'3.3.1'

我的路径中也有geckodriver可执行文件:

geckodriver.exe --version
geckodriver 0.15.0

我正在执行the official Getting Started guide for Selenium

中的步骤
>>> from selenium import webdriver
>>> from selenium.webdriver.common.keys import Keys
>>> driver = webdriver.Firefox()

但是,调用webdriver.Firefox()永远不会返回实例。它只是挂起,直到我手动点击ctrl+c,此时打印出以下堆栈跟踪:

>>> driver = webdriver.Firefox()
Traceback (most recent call last):
  File "C:\Python36-32\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\dev_env\test\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 155, in __init__
    keep_alive=True)
  File "c:\dev_env\test\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "c:\dev_env\test\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session
    response = self.execute(Command.NEW_SESSION, capabilities)
  File "c:\dev_env\test\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    response = self.command_executor.execute(driver_command, params)
  File "c:\dev_env\test\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 415, in execute
    return self._request(command_info[0], url, body=data)
  File "c:\dev_env\test\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 447, in _request
    resp = self._conn.getresponse()
  File "C:\Python36-32\lib\http\client.py", line 1331, in getresponse
    response.begin()
  File "C:\Python36-32\lib\http\client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "C:\Python36-32\lib\http\client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Python36-32\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
KeyboardInterrupt
>>>

为什么我无法按照官方指南中的说明创建browser对象?

1 个答案:

答案 0 :(得分:-1)

我假设你已经下载了geckodriver。现在你所要做的就是:

System.setProperty("webdriver.gecko.driver", path to geckodriver.exe);