Selenium不会在Python中打开浏览器

Selenium doesn't open the browser in Python

我是Python的新手,我在Debian中尝试使用Selenium,但它不起作用,更具体地说,它似乎停留在一个循环中,什么也没有发生。下一个脚本是我使用的测试:

1
2
3
4
#!/usr/bin/env python
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.python.org')

当我中断脚本时,将显示以下文本:

Traceback (most recent call last):

File"prueba_parseo.py", line 7, in browser =
webdriver.Firefox() File
"/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py",
line 154, in init
keep_alive=True)

File
"/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py",
line 140, in init
self.start_session(desired_capabilities, browser_profile)

File
"/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py",
line 229, in start_session
response = self.execute(Command.NEW_SESSION, parameters)

File
"/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py",
line 295, in execute
response = self.command_executor.execute(driver_command, params)

File
"/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py",
line 464, in execute
return self._request(command_info[0], url, body=data)

File
"/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py",
line 488, in _request
resp = self._conn.getresponse()

File"/usr/lib/python2.7/httplib.py", line 1111, in getresponse
response.begin()

File"/usr/lib/python2.7/httplib.py", line 444, in begin
version, status, reason = self._read_status()

File"/usr/lib/python2.7/httplib.py", line 400, in _read_status
line = self.fp.readline(_MAXLINE + 1)

File"/usr/lib/python2.7/socket.py", line 476, in readline
data = self._sock.recv(self._rbufsize)

KeyboardInterrupt

我一直在寻找答案,但什么也没用。我已经更改了包的版本,export no_proxy="localhost,127.0.0.1"

操作系统:Debian 5

Python:2.7

硒:3.5

壁虎司机:0.17.0

火狐:52

我不知道还能做什么或改变什么。非常感谢你!


我的猜测是,实际上一切都很顺利,浏览器是在后台启动的。它保持开放的原因可能是因为默认选项keep_alive=True,我可以在您的回溯中看到。

完成测试后,尝试使用browser.close()browser.quit()关闭浏览器。

从文档中:

Finally, the browser window is closed. You can also call quit method
instead of close. The quit will exit entire browser whereas close`
will close one tab, but if just one tab was open, by default most
browser will exit entirely.

http://selenium python.readthedocs.io/getting started.html简单用法