Selenium – A exception with a null response was thrown sending an HTTP request

本文探讨了使用TeamCity代理运行自动化测试时遇到的问题及解决尝试。错误表现为发送HTTP请求到远程WebDriver服务器时出现异常,导致测试失败。文章详细记录了排查过程,包括检查TeamCity服务状态、IE配置验证、利用Fiddler进行网络调用检查等。
[Problem]
We use teamcity agent to run automation(mstest, selenium). It always fail with below error message. But when I run automation in this agent machine loacally, it works very well.Test method EFSchools.EnglishFirst.Odin.UITest.Features.AdvancedSearch.AdvancedSearchFeature.UserCanSearchContactByAdvancedSearch threw exception:
OpenQA.Selenium.WebDriverException: A exception with a null response was thrown sending an HTTP request to the remote WebDriver server for URL Selenium   A exception with a null response was thrown sending an HTTP requesthttp://localhost:21516/session/46ed7be5-0e83-45e7-9b91-1dd961847f06/element/d1808de8-640e-4316-8f0b-3ee2b47d7c52/value. The status of the exception was ReceiveFailure, and the message was: The underlying connection was closed: An unexpected error occurred on a receive. —> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. —> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. —> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host

[Things I tried]

1. check wether teamcity agent service is started.
2. IE configuration is correct.
– security consistant in each level
– clear history when close browser
3. User fiddler to check every call ,since we can’t view the running display via teamcity
4. IE 11 is not supported with current web driver
5. narrow down the issue wether it’s repro on other browser(e.g: chrom or firefox)

<think>好的,用户的问题是关于在Selenium DevTools v119中,ResponseReceived事件的requestId是否存在重复的情况。我需要先理解用户的具体需求和背景。 首先,用户可能在自动化测试中使用Selenium,特别是通过DevTools Protocol来监听网络事件。他们可能遇到了某些情况下requestId重复的问题,导致数据处理错误。或者他们想确保在分析网络请求时,每个requestId是唯一的,以便正确跟踪请求和响应。 接下来,我需要回忆Selenium DevTools的相关知识。DevTools Protocol中的Network域确实有RequestWillBeSent和ResponseReceived事件,每个网络请求都有一个唯一的requestId。根据官方文档,这个ID应该是唯一的,用于关联请求和响应。 然后,考虑到用户提到的v119版本,可能需要检查该版本是否有变更记录,是否存在已知的问题。不过,通常来说,requestId的设计就是为了唯一标识请求,所以重复的可能性较低。不过,用户可能遇到了特殊情况,比如页面跳转、重定向或服务端推送等,导致ID重复。 还需要考虑用户的实际使用场景。例如,是否在监听事件时正确地处理了异步情况,或者是否有缓存或重试机制导致相同的requestId被多次使用。此外,跨页面或跨框架的请求是否会影响requestId的唯一性。 然后,我需要验证这些可能性。查阅官方文档或社区讨论,确认requestId的唯一性保证。例如,Chrome DevTools Protocol的文档明确指出,每个请求的requestId是唯一的,并且在页面生命周期内不会重复。因此,在正常情况下,用户不应遇到重复的情况。 如果用户确实遇到了重复的requestId,可能的原因包括:浏览器扩展干扰、Selenium版本问题、或者代码逻辑错误,比如事件监听器被多次绑定,导致同一事件被处理多次,从而误认为ID重复。此外,网络重定向或服务端推送(如HTTP/2)可能有不同的处理方式,需要确认这些情况下的ID生成机制。 最后,建议用户检查代码,确保正确使用事件监听,避免重复绑定。同时,可以编写测试代码,模拟多次请求,观察requestId是否重复。如果问题依旧,可能需要进一步调试或提交问题到Selenium的GitHub仓库。</think>在 Selenium DevTools v119 中,`Network.responseReceived` 事件的 `requestId` 字段设计上应当保持唯一性。根据 Chrome DevTools Protocol 规范,每个网络请求的 `requestId` 在浏览器会话中是全局唯一的标识符,用于精确匹配请求与响应事件[^3]。 ### 重复性分析结论 1. **理论层面** `requestId` 通过浏览器内核的请求队列管理机制生成,采用递增编号或哈希算法确保唯一性。在标准实现中,不会出现跨请求的重复标识符[^3]。 2. **实践验证** 可通过以下代码片段验证: ```python from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Chrome() devtools = driver.devtools devtools.send("Network.enable") request_ids = set() def log_response(event): request_id = event["params"]["requestId"] if request_id in request_ids: print(f"重复 requestId 发现: {request_id}") else: request_ids.add(request_id) devtools.add_listener("Network.responseReceived", log_response) # 触发多个请求 driver.get("https://example.com") driver.find_element(By.TAG_NAME, "a").click() driver.quit() ``` 此代码会检测到重复的 `requestId` 才会输出提示(正常情况无输出) ### 特殊场景例外 以下两种特殊情况可能导致 ID 复用: 1. **HTTP/2 Server Push** 服务器主动推送资源时可能复用主请求的 `requestId`(需配合`Network.requestWillBeSent`事件类型过滤) 2. **页面重定向链** 重定向过程中浏览器可能复用原始请求 ID(可通过`Network.requestWillBeSent`的`redirectResponse`字段识别) ### 调试建议 1. 使用 `Network.requestWillBeSent` + `Network.responseReceived` 事件组合验证请求生命周期 2. 检查 Selenium 4 与 Chrome 119 版本的兼容性 3. 通过浏览器原生开发者工具验证相同操作下的请求 ID 生成情况
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值