作者:一切皆空2502861573 | 来源:互联网 | 2022-12-09 13:26
出于某种原因,只有在打开嵌套webdriver
实例时才会出现以下错误.不知道这里发生了什么.
我使用的是Windows 10, geckodriver 0.21.0和Python 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
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
有没有人知道可能导致连接中止的原因是什么?
1> DebanjanB..:
此错误消息...
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保持活动连接
解
第一步将确定是否有任何软件阻止与计算机中服务器的连接。除此之外,可能的解决方案是:
禁用防病毒软件。
禁用防火墙。
确保系统上的/ etc / hosts包含以下条目:
127.0.0.1 localhost.localdomain localhost
由于连接被主机中的软件中止,因此您需要允许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
刚刚被释放。如果您受到此问题的影响,请相应升级。
参考文献:
带请求的烧瓶破损管
跨域资源共享(CORS)
连接被主机中的软件中止
[WinError 10053]带有0.21.0
闲置5s后,无需使用Selenium Python客户端重新连接,与geckodriver 0.21.0的保持活动连接断开
支持保持活动连接
结构超级::服务器::服务器
urllib3