为何出现CDP版本111无匹配返回109的警告?Selenium配置求助
为什么Chrome 111会出现CDP版本匹配到109的警告?
问题描述
运行Selenium自动化代码时出现警告:WARNING: Unable to find an exact match for CDP version 111,系统自动返回最接近的CDP版本109,想了解该问题的原因。
环境信息
Chrome浏览器版本
111.0.5563.64
pom.xml依赖配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>SeleniumAutomationAssertions</groupId> <artifactId>SeleniumAutomationAssertions</artifactId> <version>0.0.1-SNAPSHOT</version> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <release>17</release> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.8.0</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-http-jdk-client</artifactId> <version>4.8.0</version> </dependency> </dependencies> </project>
执行代码
package testcases; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import Verify.Compare; public class ValidateElementExistsByXpath { WebDriver driver; boolean result; @BeforeMethod public void createDriver() { System.setProperty("webdriver.http.factory", "jdk-http-client"); System.setProperty("webdriver.chrome.driver", "/Users/imtiyaz/Documents/chromedriver"); driver = new ChromeDriver(); driver.get("https://www.facebook.com"); driver.findElement(By.xpath("//button[text()='Allow essential and optional cookies']")).click(); ChromeOptions options = new ChromeOptions(); options.addArguments("--remote-allow-origins=*"); WebDriver driver = new ChromeDriver(options); } @Test public void testcase3() { driver.findElement(By.id("email")).sendKeys("Hello"); Compare.ValidateElementExists(driver, "//input[@id='pass']"); //This is the xpath which will be checked, will return either true or false, so I'll set 'boolean result' above Assert.assertTrue(result); //I'm expecting that the element should exist, hence using assertTrue } }
已尝试的操作
- 已在pom.xml中添加
selenium-http-jdk-client依赖 - 代码中设置了
System.setProperty("webdriver.http.factory", "jdk-http-client"); - 已将Chrome浏览器更新至最新版本
- 已将
selenium-java和selenium-http-jdk-client版本升级至4.8.0
原因及解决办法
原因
Selenium 4.8.0内置支持的Chrome DevTools Protocol(CDP)版本最高为109,而你使用的Chrome 111对应的CDP版本不在该版本Selenium的支持列表中,因此系统会自动降级匹配最接近的CDP版本109,从而触发该警告。
解决办法
- 升级Selenium版本:将
selenium-java和selenium-http-jdk-client版本升级至4.9.0及以上,因为从Selenium 4.9.0开始,官方已添加对Chrome 111对应CDP版本的支持,升级后警告会消失。 - 修正代码中的实例化问题:你的
createDriver方法中两次实例化了ChromeDriver,第二次的局部变量driver覆盖了类成员变量,会导致@Test方法中的driver为空,引发空指针异常。需要删除第二次的WebDriver driver = new ChromeDriver(options);,改为直接给成员变量赋值:
或者将ChromeOptions配置提前到第一次实例化之前,避免重复创建浏览器实例。ChromeOptions options = new ChromeOptions(); options.addArguments("--remote-allow-origins=*"); // 删除局部变量声明,直接使用成员变量 driver = new ChromeDriver(options);
内容的提问来源于stack exchange,提问作者Immy110
相关产品推荐
相关产品推荐

