ConnectionAbortedError:\[WinError 10053\] 已建立的连接被主机中的软件中止

新手上路,请多包涵

出于某种原因, 只有 当我打开嵌套的 webdriver 实例时,我才会收到以下错误。不知道这里发生了什么。

我正在使用 Windows 10、 geckodriver 0.21.0Python 3.7。

ConnectionAbortedError:[WinError 10053]

 An established connection was aborted by the software in your host machine

运行良好的脚本的一部分

tab_backers = ff.find_element_by_xpath('//a[@gogo-test="backers_tab"]')

try:
    funding_backers_count = int(''.join(filter(str.isdigit, str(tab_backers.text))))
except ValueError:
    funding_backers_count = 0

if funding_backers_count > 0:
    tab_backers.click()

    see_more_backers = WebDriverWait(ff, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//ui-view//a[text()="See More Backers"]'))
    )
    clicks = 0
    while clicks < 0:
        clicks += 1
        ff.WebDriverWait(ff, 5).until(
            see_more_backers.click()
        )

    for container in ff.find_elements_by_xpath('//ui-view//div[@class="campaignBackers-pledge ng-scope"]'):
        backers_profile = container.find_elements_by_xpath('./*/div[@class="campaignBackers-pledge-backer-details"]/a')
        if len(backers_profile) > 0:
            backers_profile = backers_profile[0].get_attribute('href')
        else:
            backers_profile = 'Unknown'
        backers_name = safe_encode(container.find_element_by_xpath('(./*/div[@class="campaignBackers-pledge-backer-details"]/*)[1]').text)
        backers_timestamp = container.find_element_by_xpath('./*/div[@class="campaignBackers-pledge-backer-details"]/div[contains(@class, "campaignBackers-pledge-backer-details-note")]').text
        backers_contribution = container.find_element_by_xpath('./*//*[contains(@class, "campaignBackers-pledge-amount-bold")]').text
        if backers_contribution != 'Private':
            backers_contribution = int(''.join(filter(str.isdigit, str(backers_contribution))))
        if backers_profile != 'Unknown':

部分脚本导致系统中止连接

            _ff = create_webdriver_instance()
            _ff.get(backers_profile)
            _ff.quit()

追溯

Traceback (most recent call last):
  File "C:\Users\Anthony\Desktop\test.py", line 271, in <module>
    backers_profile = container.find_elements_by_xpath('./*/div[@class="campaignBackers-pledge-backer-details"]/a')
  File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 381, in find_elements_by_xpath
    return self.find_elements(by=By.XPATH, value=xpath)
  File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 680, in find_elements
    {"using": by, "value": value})['value']
  File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 318, in execute
    response = self.command_executor.execute(driver_command, params)
  File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\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\Anthony\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 495, in _request
    self._conn.request(method, parsed_url.path, body, headers)
  File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1055, in _send_output
    self.send(chunk)
  File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 977, in send
    self.sock.sendall(data)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

geckodriver.log文件

它在 代码笔中, 因为它太长了!

create_webdriver_instance 函数

def create_webdriver_instance():
    options = Options()
    options.add_argument('-headless')
    try:
        ua_string = random.choice(ua_strings)
        profile = webdriver.FirefoxProfile()
        profile.set_preference('general.useragent.override', ua_string)
        return webdriver.Firefox(profile) # profile, firefox_options=options
    except IndexError as error:
        print('\nSection: Function to Create Instances of WebDriver\nCulprit: random.choice(ua_strings)\nIndexError: {}\n'.format(error))
        return webdriver.Firefox() # firefox_options=options

有没有人知道什么可能导致连接中止?

原文由 oldboy 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 3.5k
1 个回答

这个错误信息…

 ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

…意味着新的 WebBrowsing 会话(即 Firefox 浏览器 会话)的初始化已中止。


已建立的连接被主机中的软件中止

根据您的代码尝试,错误显然会出现 create_webdriver_instance() 函数,其中包含:

 try:
    ua_string = random.choice(ua_strings)
    profile = webdriver.FirefoxProfile()
    profile.set_preference('general.useragent.override', ua_string)
    return webdriver.Firefox(profile)

和:

 except IndexError as error:
    print('\nSection: Function to Create Instances of WebDriver\nCulprit: random.choice(ua_strings)\nIndexError: {}\n'.format(error))
    return webdriver.Firefox()

因此,您在 return webdriver.Firefox(profile)webdriver.Firefox() 中面临的问题并不完全清楚。

也许仔细查看 codepen 中的日志表明错误来自 webdriver.Firefox(profile)


原因

此错误背后可能有多种原因:

  • 防病毒软件 的存在。
  • 防火墙阻塞端口。
  • 网络配置。
  • 问题可能是由 CORS 引起的。
  • 由于启用了 HTTP keep-alive 连接

解决方案

第一步是找出是否有任何软件阻止了与您计算机中服务器的连接。除此之外,可能的解决方案是:

  • 禁用 _防病毒软件_。
  • 禁用 _防火墙_。
  • 确保系统上的 /etc/hosts 包含以下条目:
   127.0.0.1   localhost.localdomain localhost

  • 根据 连接被主机中的软件中止, 您需要允许本地主机路由,如 http://localhost:8080/reactive-commands

  • 根据 Keep-Alive 与 geckodriver 0.21.0 的连接在 5 秒不活动后断开,无需使用 Selenium Python 客户端重新连接

    • AutomatedTester :这个问题不是因为我们在做请求时没有连接。如果那是问题所在,我们将抛出一个 httplib.HTTPConnection 异常。相反,当我们建立连接并关闭它并尝试解析响应时,会抛出 BadStatusLine。现在这可能是 python stdlib 错误、httplib 错误或 selenium 错误。用 Keep-Alive 连接不会表现出相同缺陷的其他东西替换 urllib 的 Python 客户端是 WIP

    • andreastt :geckodriver 团队正在努力将服务器端超时值扩展到更合理的值。正如我所说,这将有助于缓解这个问题,但不能从根本上解决它。在任何情况下,5 秒对于从持久 HTTP 连接中获得真正的好处来说可能太短了,将它增加到 60 秒左右会获得更好的性能。


结论

Selenium 3.14.0 刚刚发布。如果您受到此问题的影响,请相应升级。


参考:

原文由 undetected Selenium 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题