学习 selenium 时,如果 sleep 时间大于等于 5 秒,就会提示 ConnectionResetError: [Errno 54] Connection reset by peer。
换成 chrome 浏览器,可以正常运行。

#demo.py
from selenium import webdriver
from time import sleep, ctime
#chrome
#driver = webdriver.Chrome(executable_path='//Users/csj/Desktop/seleniumdriver/chromedriver/chromedriver')
#Firefoxdriver 21.0
driver = webdriver.Firefox(executable_path='//Users/csj/Desktop/seleniumdriver/firefoxdriver/21.0/geckodriver')
#Firefoxdriver 20.1
#driver = webdriver.Firefox(executable_path='//Users/csj/Desktop/seleniumdriver/firefoxdriver/20.1/geckodriver')
#Firefoxdriver 20.0
#driver = webdriver.Firefox(executable_path='//Users/csj/Desktop/seleniumdriver/firefoxdriver/20.0/geckodriver')
#Firefox 19.1
#driver = webdriver.Firefox(executable_path='//Users/csj/Desktop/seleniumdriver/firefoxdriver/19.1/geckodriver')
driver.implicitly_wait(10)
driver.get("http://www.baidu.com")
print(ctime())
sleep(5)
print(ctime())
driver.find_element_by_id("kw").send_keys("selenium")
driver.close()```
使用Firefoxdriver 21.0报如下错误
Traceback (most recent call last):
  File "/Users/csj/PycharmProjects/untitled/demo.py", line 13, in <module>
    driver.find_element_by_id("kw").send_keys("selenium")
  File "/Users/csj/anaconda/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 353, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "/Users/csj/anaconda/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 957, in find_element
    'value': value})['value']
  File "/Users/csj/anaconda/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/Users/csj/anaconda/lib/python3.6/site-packages/selenium/webdriver/remote/remote_connection.py", line 472, in execute
    return self._request(command_info[0], url, body=data)
  File "/Users/csj/anaconda/lib/python3.6/site-packages/selenium/webdriver/remote/remote_connection.py", line 496, in _request
    resp = self._conn.getresponse()
  File "/Users/csj/anaconda/lib/python3.6/http/client.py", line 1331, in getresponse
    response.begin()
  File "/Users/csj/anaconda/lib/python3.6/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/Users/csj/anaconda/lib/python3.6/http/client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/Users/csj/anaconda/lib/python3.6/socket.py", line 586, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [Errno 54] Connection reset by peer

换成chrome可以正常运行老板提醒可能是版本的问题分别用 Firefox 20.1, 20.0, 19.1,均能运行成功没有再提示ConnectionResetError
休眠时间设置为10秒也没有再报错


↙↙↙阅读原文可查看相关链接,并与作者交流