1

我在 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

我正在按照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按照官方指南中的说明创建对象?

4

1 回答 1

-1

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

System.setProperty("webdriver.gecko.driver", path to geckodriver.exe);
于 2017-03-29T18:20:19.947 回答