我使用的是Ubuntu x64
当我执行以下代码时:
from selenium import webdriver
x = webdriver.Firefox()
x.get('any address')
我得到了一个错误的错误:
% python seleniumtest.py
Traceback (most recent call last):
File "seleniumtest.py", line 2, in <module>
x = webdriver.Firefox()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 85, in __init__
keep_alive=True)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 90, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 177, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 234, 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 401, 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 432, in _request
resp = self._conn.getresponse()
File "/usr/lib/python2.7/httplib.py", line 1051, in getresponse
response.begin()
File "/usr/lib/python2.7/httplib.py", line 415, in begin
version, status, reason = self._read_status()
File "/usr/lib/python2.7/httplib.py", line 371, 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)
socket.error: [Errno 104] Connection reset by peer
这是什么问题?我该怎么办呢?
我解决了这个问题在终端执行fallowing命令:
终端1:% sudo Xvfb :10 -ac
终端2:$ export DISPLAY=:10
执行以下代码:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()
返回此错误:
% python seleniumtest.py
Traceback (most recent call last):
File "seleniumtest.py", line 4, in <module>
driver = webdriver.Firefox()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 81, in __init__
self.binary, timeout)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 51, in __init__
self.binary.launch_browser(self.profile, timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 68, in launch_browser
self._wait_until_connectable(timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 98, in _wait_until_connectable
raise WebDriverException("The browser appears to have exited "
selenium.common.exceptions.WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.
我的Firefox
和Selenium
版本:
% firefox -v
Mozilla Firefox 47.0
% pip show selenium
---
Metadata-Version: 2.0
Name: selenium
Version: 2.53.5
Summary: Python bindings for Selenium
Home-page: https://github.com/SeleniumHQ/selenium/
Author: UNKNOWN
Author-email: UNKNOWN
Installer: pip
License: UNKNOWN
Location: /usr/local/lib/python2.7/dist-packages
Requires:
Classifiers:
Development Status :: 5 - Production/Stable
Intended Audience :: Developers
License :: OSI Approved :: Apache Software License
Operating System :: POSIX
Operating System :: Microsoft :: Windows
Operating System :: MacOS :: MacOS X
Topic :: Software Development :: Testing
Topic :: Software Development :: Libraries
Programming Language :: Python
Programming Language :: Python :: 2.6
Programming Language :: Python :: 2.7
Programming Language :: Python :: 3.2
Programming Language :: Python :: 3.3
Programming Language :: Python :: 3.4
答案 0 :(得分:0)
尝试使用以下代码:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
try:
driver = webdriver.Firefox()
except:
binary = FirefoxBinary('C:/Program Files (x86)/Mozilla Firefox/firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary)
也可能是你的Mozilla Firefox版本有问题,当我的Firefox自动更新时,webdriver.Firefox()
对我不起作用所以我不得不将Firefox版本降级到36.0
答案 1 :(得分:0)
也许你需要Firefoxdriver for selenium。我使用Chrome进行硒,然后下载Chromedriver。您可以从https://sites.google.com/a/chromium.org/chromedriver/downloads下载。 你可以参考https://sites.google.com/a/chromium.org/chromedriver/getting-started。
from selenium import webdriver
import time
chrome_driver_path = "/home/shifeng/shifengworld/tool/chrome_driver_downloads/chromedriver"
browser = webdriver.Chrome(executable_path = chromedriver_path)
href = "http://stackoverflow.com"
browser.get(href)
time.sleep(2)
我的需要是可以的。