python3+selenium 3.13 + geckodriver 21.0,提示ConnectionResetError,切换会较低版本的driver即可

Posted csj2018

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3+selenium 3.13 + geckodriver 21.0,提示ConnectionResetError,切换会较低版本的driver即可相关的知识,希望对你有一定的参考价值。

学习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秒,也没有再报错。

以上是关于python3+selenium 3.13 + geckodriver 21.0,提示ConnectionResetError,切换会较低版本的driver即可的主要内容,如果未能解决你的问题,请参考以下文章

python3 安装selenium

selenium3+python3

Python3 Selenium学习

Selenium+Python3环境配置

selenium+python3环境准备

Selenium---Python3---弹框处理