Selenium Grid搭配Chrome113在MacOS执行测试时抛出加载状态异常
我在MacOS 10.15 Catalina系统上,使用Selenium Grid(selenium-server-standalone.3.14.0)搭配chromedriver 113.0.5672.63,基于NUnit运行Selenium测试。部分测试能成功通过,但有部分随机测试在调用IWebElement.Click()时失败,报错信息如下:
unknown error: cannot determine loading status
from unknown error: cannot determine loading status
from no such window
(Session info: chrome=113.0.5672.92).Exception Stack Trace:
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary2 parameters) at OpenQA.Selenium.Remote.RemoteWebElement.Execute(String commandToExecute, Dictionary2 parameters)
我怀疑这和Chrome 113的更新有关,类似TestComplete也出现过Chrome 113相关的兼容性问题。
- 升级Selenium版本:Selenium 3.14.0版本过旧,对Chrome 113这类新版本浏览器兼容性不足。建议升级到Selenium 4.x系列(如4.9.0及以上),新版本修复了大量高版本Chrome的兼容问题,可有效避免窗口状态识别错误。
- 严格匹配Chrome与ChromeDriver版本:当前本地Chrome版本为113.0.5672.92,需下载完全同版本的ChromeDriver替换现有113.0.5672.63版本,版本不匹配会引发会话执行异常。
- 添加显式等待机制:在点击操作前等待元素可点击且页面加载稳定,避免因页面未就绪导致的错误:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); IWebElement targetElement = wait.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("your-element-selector"))); targetElement.Click(); - 添加Chrome启动参数:尝试添加
--disable-features=VizDisplayCompositor参数,可解决Chrome 113中部分渲染相关的会话问题:ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.AddArgument("--disable-features=VizDisplayCompositor"); IWebDriver driver = new RemoteWebDriver(new Uri("http://your-grid-hub-url:4444/wd/hub"), chromeOptions); - 检查Selenium Grid环境一致性:确保Grid的Hub和所有节点使用相同版本的Selenium,且每个节点上的Chrome、ChromeDriver版本完全匹配,避免因环境不一致导致的随机失败。
内容的提问来源于stack exchange,提问作者IndianCodeMaster

