Chrome浏览器更新后执行Selenide失败的问题

很长一段时间没有使用Selenide进行网页测试了,找出来原来的Selenide程序运行。由于系统升级及浏览器Chrome的不断更新造成原来的测试无法成功运行。

通过查找输出的日志发现问题出在Chrome启动后,出现了403错误。通过分析查阅网上资料发现,在Chrome浏览器升级到113版本之后,就会出现403造成Selenide无法正常进行测试。

ChromeDriver was started successfully on port 65228.
十月 28, 2025 11:48:20 下午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Detected dialect: W3C
十月 28, 2025 11:48:20 下午 org.openqa.selenium.remote.http.WebSocket$Listener onError
警告: Invalid Status code=403 text=Forbidden
java.io.IOException: Invalid Status code=403 text=Forbidden
	at org.asynchttpclient.netty.handler.WebSocketHandler.abort(WebSocketHandler.java:92)
	at org.asynchttpclient.netty.handler.WebSocketHandler.handleRead(WebSocketHandler.java:118)

找到Selenide官方给出的解决方案如下:https://selenide.org/2023/03/09/selenide-6.12.2/

We recommend you upgrading to HttpClient. Yes, it requires Java11+, but has better support etc.

给出的解决方案就是升级Java版本及其内部的HttpClient。由于我使用的Java8因为原有程序环境的问题暂时无法升级,这一方法没有测试不知道是否成功

如果还是使用现有的Java8还能不能使用Chrome113之后的版本进行测试呢,经过一番研究与测试之后,发现使用如下方法可解决这个问题。

自己定义ChromeDriver参数,具体代码如下所示:

Configuration.baseUrl = "http://localhost:8080/webfast/";
String rootPath = System.getProperty("user.dir");

//1、参数设置,无头模式
ChromeOptions options = new ChromeOptions();
//2、隐藏 navigator.webdriver
options.addArguments("--disable-blink-features=AutomationControlled");
//3、解决403错误
options.addArguments("--remote-allow-origins=*");
 //4、关闭左上方Chrome 正受到自动测试软件的控制的提示
options.setExperimentalOption("useAutomationExtension", false);
ChromeDriver driver = new ChromeDriver(options);
setWebDriver(driver);
System.setProperty("webdriver.chrome.driver", rootPath + "/webdriver/chrome/chromedriver.exe");
Configuration.browser = "chrome";

经过上面修改,本以为会成功完成测试,结果又问题了,查看日志发现出现了如下错误:

The path to the driver executable The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from https://chromedriver.storage.googleapis.com/index.html
java.lang.IllegalStateException: The path to the driver executable The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from https://chromedriver.storage.googleapis.com/index.html
	at org.openqa.selenium.internal.Require$StateChecker.nonNull(Require.java:311)

The path to the driver executable must be set by the webdriver.ie.driver system property;错误的原因是设置ChromeDriver可执行程序的路径放在在ChromeDriver初始化的后面。

解决的方法是:
需要先使用System.setProperty 去设置ChromeDriver 路径,再去创建 ChromeDriver
具体是代码:
        String rootPath = System.getProperty("user.dir");
        System.setProperty("webdriver.chrome.driver", rootPath + "/webdriver/chrome/chromedriver.exe");

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--disable-blink-features=AutomationControlled");
        options.addArguments("--remote-allow-origins=*");
        options.setExperimentalOption("useAutomationExtension", false);
        ChromeDriver driver = new ChromeDriver(options);
        setWebDriver(driver);
        Configuration.browser = "chrome";

经过上面的调整后,久违的Selenide测试终于成功了。

这一过程参考的网上资源记录如下:

【Java自动化干货】Selenide太难?这样做简洁又高级,学会了工资翻一倍  https://zhuanlan.zhihu.com/p/427504668

Java Selenide 介绍&使用    https://www.cnblogs.com/juno3550/p/15827600.html

https://github.com/eugenp/tutorials/blob/master/testing-modules/selenide/src/test/java/com/baeldung/selenide/SearchLiveTest.java Selenide代码示例

selenium操作谷歌浏览器,驱动使用最新版113版本,启动程序报403解决方式 https://blog.csdn.net/qq_52545155/article/details/130830515

setWebDriver or WebDriverProvider? https://selenide.org/2019/12/03/advent-calendar-set-webdriver-vs-webdriver-provider/?ref=hackandslash.blog

Selenide - Create a Custom WebDriver https://hackandslash.blog/selenide-webdriverfactory/

Selenide 介绍 https://www.baeldung-cn.com/selenide

Java获取项目根目录路径的6种实现方法 https://i.cnblogs.com/posts/edit

Java Selenide 简介与用法 https://www.zhangshengrong.com/p/zD1yDJWWXr/

Why is My @BeforeClass Method Not Executing in JUnit Tests? https://codingtechroom.com/question/why-beforeclass-method-not-executing-junit

Springboot集成selenide 自动化测试 https://blog.csdn.net/q11604316081/article/details/79392959

How to Fix No Tests Found for Given Includes Error in Test Frameworks? https://www.browserstack.com/guide/no-tests-found-for-given-includes

selenium缺少chromedriver解决方法 https://www.cnblogs.com/shenh/p/10070790.html

如何在Chrome浏览器中使用ChromeDriver执行Selenide? https://dev59.com/4ovda4cB1Zd3GeqPetfh

 补充说一下,很长时间没有使用JUnit进行测试了,有些细节就会遗忘,在最开始进行测试时使用@BeforeClass方法后的测试用例都发生了失败,找了许久也没发现问题所在,直到看到JUnit测试的方法签名中@BeforeClass注解的方法必须是static静态的。而我在编写测试的时测试类的初始构建方法没有写成静态的。不是疏忽,而是遗忘了(汗......)

posted @ 2025-10-29 00:28  培轩  阅读(11)  评论(0)    收藏  举报