Chrome升级后Selenium测试报错:Cannot find context with specified id
根据你提供的错误信息和环境细节,我判断核心问题出在Chrome浏览器与Chromedriver的版本不兼容,同时给你几个额外的排查优化方向,帮你快速解决问题:
1. 优先修复版本兼容性问题
Chromedriver和Chrome的版本是严格绑定的:
- 你当前用的Chromedriver 2.34仅支持Chrome 62版本
- 而你的Chrome已经升级到63.0.3239.108,需要搭配Chromedriver 2.35(该版本支持Chrome 62-64全系列)
直接下载对应版本的Chromedriver替换项目中的驱动文件,这应该能解决Cannot find context with specified id这个核心报错。
2. 验证Frame切换的有效性
虽然你提到已经切换到目标frame,但报错里的"context not found"也可能是切换未生效导致的,建议优化切换逻辑:
- 尽量避免用index定位frame(页面结构变动就会失效),改用
id、name或者直接定位frame元素的方式切换:// 示例:通过元素定位frame并切换 IWebElement targetFrame = driver.FindElement(By.Id("你的frameId")); driver.SwitchTo().Frame(targetFrame); - 切换后立即做个小验证,确认是否进入了目标frame:
// 检查frame内是否存在某个已知元素 bool isInTargetFrame = driver.FindElements(By.Id("frame内元素ID")).Count > 0; - 如果是嵌套frame,需要逐层切换,不要直接跳转到内层frame。
3. 替换ExpectedConditions为自定义等待逻辑
旧版本的ExpectedConditions可能存在兼容性问题,你可以改用更灵活稳定的自定义等待:
// 替换原来的Wait.Until(ExpectedConditions.ElementExists(findElement)) var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15)); IWebElement targetElement = wait.Until(driver => { try { var element = driver.FindElement(findElement); // 确认元素可见再返回 return element.Displayed ? element : null; } catch (NoSuchElementException) { return null; } });
4. 清理Chrome环境残留
Chrome升级后可能残留旧进程或缓存,干扰驱动建立上下文:
- 关闭所有Chrome窗口,打开任务管理器杀掉所有
chrome.exe和chromedriver.exe进程 - 清理Chrome浏览器缓存(设置→隐私和安全→清除浏览数据)
- 重启测试项目再运行
你的错误堆栈信息(方便后续排查)
测试名称:TrocarTecnologiaTest
测试全名:AutomacaoIntegra.Tests.TestTrocarTecnologia.TrocarTecnologiaTest
测试来源:C:\git_repository\integra_test\AutomacaoIntegra\Tests\TestTrocarTecnologia.cs : 第39行
测试结果:失败
测试时长:0:00:16,571
结果堆栈跟踪:
em OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
em OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary2 parameters) em OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value) em OpenQA.Selenium.Remote.RemoteWebDriver.FindElementById(String id) em OpenQA.Selenium.By.<>c__DisplayClass16_0.<Id>b__0(ISearchContext context) em OpenQA.Selenium.By.FindElement(ISearchContext context) em OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by) em OpenQA.Selenium.Support.UI.ExpectedConditions.<>c__DisplayClass6_0.<ElementExists>b__0(IWebDriver driver) em OpenQA.Selenium.Support.UI.DefaultWait1.Until[TResult](Func`2 condition)
em AutomacaoIntegra.Extensions.Element_Extensions.EsperarElemento(IWebDriver driver, By findElement) na C:\git_repository\integra_test\AutomacaoIntegra\Extensions\Element_Extensions.cs:linha 126
em AutomacaoIntegra.PageObjects.SolicitacaoPage.PesquisarClienteBeneficiario(String CPF) na C:\git_repository\integra_test\AutomacaoIntegra\PageObjects\SolicitacaoPage.cs:linha 117
em AutomacaoIntegra.Tests.TestTrocarTecnologia.TrocarTecnologiaTest() na C:\git_repository\integra_test\AutomacaoIntegra\Tests\TestTrocarTecnologia.cs:linha 48
结果消息:System.InvalidOperationException : unknown error: unhandled inspector error: {"code":-32000,"message":"Cannot find context with specified id"} (Session info: chrome=63.0.3239.108) (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 10.0.14393 x86_64)
内容的提问来源于stack exchange,提问作者Eduardo Alexandre de Souza

