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

Pri*_*Nom 14 python selenium python-3.x selenium-webdriver geckodriver

出于某种原因,只有在打开嵌套webdriver实例时才会出现以下错误.不知道这里发生了什么.

我使用的是Windows 10, geckodriver 0.21.0Python 3.7.

ConnectionAbortedError:[WinError 10053]

An established connection was aborted by the software in your host machine
Run Code Online (Sandbox Code Playgroud)

工作正常的脚本的一部分

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':
Run Code Online (Sandbox Code Playgroud)

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

            _ff = create_webdriver_instance()
            _ff.get(backers_profile)
            _ff.quit()
Run Code Online (Sandbox Code Playgroud)

追溯

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)


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


And*_*kov 8

正如文档所说:

软件导致连接中止。已建立的连接被主机中的软件中止,可能是由于数据传输超时或协议错误。

可能的原因:

  1. 超时或其他网络级错误。
  2. 网络连接中断
  3. 防火墙关闭了连接,因为它打开的时间太长
  4. 在进程完成之前连接已关闭
  5. 防病毒软件阻止连接

等等。

也尝试降级geckodriver 0.21.0geckodriver 0.20.1. 你可以在这里下载。/sf/answers/3586570361/好像有问题geckodriver 0.21.0

PS:

options.add_argument('-headless')
Run Code Online (Sandbox Code Playgroud)

应该:

options.add_argument('--headless')
Run Code Online (Sandbox Code Playgroud)


Deb*_*anB 6

此错误消息...

ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
Run Code Online (Sandbox Code Playgroud)

...表示新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)
Run Code Online (Sandbox Code Playgroud)

和:

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

因此,您不清楚在return webdriver.Firefox(profile)或中哪个功能要面对这个问题webdriver.Firefox()

也许仔细看一下Codepen中的日志,表明错误来自webdriver.Firefox(profile)


原因

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

  • 存在防病毒软件
  • 防火墙阻止端口。
  • 网络配置。
  • 问题可能是由CORS引起的。
  • 由于启用了HTTP保持活动连接

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

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

    127.0.0.1   localhost.localdomain localhost
    
    Run Code Online (Sandbox Code Playgroud)
  • 由于连接被主机中的软件中止,因此您需要允许localhost路由,例如http://localhost:8080/reactive-commands

  • 根据与Geckodriver 0.21.0的Keep-Alive连接,在闲置5s后无需使用Selenium Python客户端重新连接就掉线了

    • AutomatedTester:此问题不是因为在进行请求时我们尚未连接。如果是这个问题,我们将收到一个抛出的httplib.HTTPConnection异常。相反,当我们进行连接并关闭并尝试解析响应时,将引发BadStatusLine。现在这可能是python stdlib错误,httplib错误或selenium错误。一个Python客户端与别的东西没有表现出与保持活动连接相同的缺陷是取代的urllib WIP

    • andreastt:该geckodriver团队正在扩展服务器端超时值会更合理。正如我所说,这将有助于缓解此问题,但不能从根本上解决它。无论如何,五秒钟可能太短而无法从持久的HTTP连接中获得真正的好处,将其增加到60秒左右将具有更高的性能。


结论

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


参考文献:


小智 6

这个问题发生在我身上,由于错误是间歇性的,我最初认为这是一些防火墙或防病毒问题,但它更简单。

单击“发送”按钮时,我有一个表单被提交了两次。该按钮被设置为 type="submit" 并且当单击该按钮时一个 javascript 代码提交了这个表单。我将按钮更改为 type="button",问题就解决了。