Selenium 4和NetworkInterceptor问题

6yt4nkrj  于 10个月前  发布在  其他
关注(0)|答案(1)|浏览(233)

在探索Selenium 4中拦截网络请求的开头,我尝试使用一些示例代码。
Selenium版本:4.3.0 ChromeDriver版本:109.0.5414.74
代码示例:

ChromeDriver driver = new ChromeDriver();

NetworkInterceptor interceptor = new NetworkInterceptor(
    driver, Route.matching(req -> true)
        .to(() -> req -> new HttpResponse()
            .setStatus(200)
            .addHeader("Content-Type", MediaType.HTML_UTF_8.toString())
            .setContent(utf8String("Creamy, delicious cheese!"))));

    driver.get("https://example-sausages-site.com");

    String source = driver.getPageSource();

    assertThat(source).contains("delicious cheese!");

字符串
尝试使用NetworkInterceptor时发生此错误:

org.openqa.selenium.devtools.DevToolsException: You are using a no-op implementation of
the CDP. The most likely reason for this is that Selenium was unable to find an 
implementation of the CDP protocol that matches your browser. Please be sure to include an 
implementation on the classpath, possibly by adding a new (maven) dependency of 
`org.seleniumhq.selenium:selenium-devtools-vNN:4.3.0` where `NN` matches the major version of 
the browser you're using.
Build info: version: '4.3.0', revision: 'a4995e2c09*'
System info: host: 'frank-ulyana-xfce', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', 
os.version: '5.15.0-58-generic', java.version: '11.0.17'
Driver info: DevTools Connection


我想这是版本问题?NetworkInterceptor版本不支持ChromeDriver 109?
如果是,我可以下载版本108并直接示例化它,同时等待支持109的版本吗?
我使用的是Maven,所以所有的版本和依赖都在那里管理。

zte4gxcn

zte4gxcn1#

根据4.3.0更改日志,此版本支持Chrome DevTools协议(CDP)版本85、101、102和103。但是,您当前使用的是CDP版本109和ChromeDriver。
错误消息中的此版本差异是由于Selenium版本不支持您的ChromeDriver中包含的CDP版本。您需要确保您的Selenium和ChromeDriver版本(及其底层CDP版本)兼容。
您应该密切关注CHANGELOG,以验证未来Selenium和CDP版本的兼容性。当您使用Maven时,这将帮助您更有效地管理和调整依赖项的版本。
要解决此问题,您需要使用Selenium 4.8.x变体,因为根据Selenium CHANGELOG,这些版本已确认支持CDP版本109。

相关问题