ConnectionResetError:[WinError 10054] Firefox上的硒导致远程主机错误强行关闭了现有连接

时间:2018-07-03 17:19:59

标签: python selenium selenium-webdriver xpath css-selectors

以下代码出现错误

代码:

import time
from selenium import webdriver
import urllib
driver = webdriver.Firefox()
driver.get('https://mycutebaby.in/contest/participant/?n=5b2b4bf83c352#_=_')
voter = driver.find_element_by_id('verification_vote_btn')
voter.click()

错误:

Traceback (most recent call last):
  File "C:\Users\Hello\Documents\kritin_votes.py", line 5, in <module>
    driver.get('https://mycutebaby.in/contest/participant/?n=5b2b4bf83c352#_=_')
  File "C:\Users\Hello\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 332, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Users\Hello\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 318, in execute
    response = self.command_executor.execute(driver_command, params)
  File "C:\Users\Hello\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 472, in execute
    return self._request(command_info[0], url, body=data)
  File "C:\Users\Hello\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 496, in _request
    resp = self._conn.getresponse()
  File "C:\Users\Hello\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1331, in getresponse
    response.begin()
  File "C:\Users\Hello\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "C:\Users\Hello\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Users\Hello\AppData\Local\Programs\Python\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

1 个答案:

答案 0 :(得分:0)

要单击文本为点击投票!的元素,您需要诱使 WebDriverWait 使所需的元素可点击,您可以使用以下任一解决方案:

  • CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.verification_vote-btn.vote_fb_btn#verification_vote_btn span.text"))).click()
    
  • XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='verification_vote-btn vote_fb_btn' and @id='verification_vote_btn']//span[contains(.,'Tap To Vote !')]"))).click()