Java.io.IOException:无效状态代码=403 text=Forbidden
当我尝试对远程服务器的 Azure DevOps 驱动程序执行一组 Selenium Webdriver 测试时,出现此错误堆栈:
2023-03-08T21:06:46.9827484Z Running Test 66728 Mobile Web - Pick Task
2023-03-08T21:06:46.9827843Z Test Description https://dev.azure.com/Corporation/%20FootPrint/_workitems/edit/66728
2023-03-08T21:06:47.2244460Z Starting ChromeDriver 111.0.5563.41 (976ef12907ef9b413c2d929c043307b415d27b9e-refs/branch-heads/5563@{#737}) on port 35235
2023-03-08T21:06:47.2248489Z Only local connections are allowed.
2023-03-08T21:06:47.2273309Z Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
2023-03-08T21:06:47.2530381Z ChromeDriver was started successfully.
2023-03-08T21:06:50.3292859Z Mar 08, 2023 4:06:50 PM org.openqa.selenium.remote.http.WebSocket$Listener onError
2023-03-08T21:06:50.3319163Z WARNING: Invalid Status code=403 text=Forbidden
2023-03-08T21:06:50.3320374Z java.io.IOException: Invalid Status code=403 text=Forbidden
2023-03-08T21:06:50.3321045Z at org.asynchttpclient.netty.handler.WebSocketHandler.abort(WebSocketHandler.java:92)
2023-03-08T21:06:50.3321498Z at org.asynchttpclient.netty.handler.WebSocketHandler.handleRead(WebSocketHandler.java:118)
2023-03-08T21:06:50.3322255Z at org.asynchttpclient.netty.handler.AsyncHttpClientHandler.channelRead(AsyncHttpClientHandler.java:78)
2023-03-08T21:06:50.3326071Z at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444)
2023-03-08T21:06:50.3334206Z at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
2023-03-08T21:06:50.3334813Z at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)
2023-03-08T21:06:50.3335472Z at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436)
2023-03-08T21:06:50.3336148Z at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:346)
2023-03-08T21:06:50.3336823Z at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:333)
2023-03-08T21:06:50.3337255Z at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:454)
2023-03-08T21:06:50.3337683Z at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:290)
2023-03-08T21:06:50.3339980Z at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
2023-03-08T21:06:50.3340463Z at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:442)
2023-03-08T21:06:50.3343324Z at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
2023-03-08T21:06:50.3344253Z at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)
2023-03-08T21:06:50.3344757Z at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
2023-03-08T21:06:50.3345262Z at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:440)
2023-03-08T21:06:50.3345959Z at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
2023-03-08T21:06:50.3346524Z at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
2023-03-08T21:06:50.3346956Z at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
2023-03-08T21:06:50.3347371Z at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:788)
2023-03-08T21:06:50.3347781Z at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724)
2023-03-08T21:06:50.3348183Z at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650)
2023-03-08T21:06:50.3348555Z at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562)
2023-03-08T21:06:50.3349009Z at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
2023-03-08T21:06:50.3349522Z at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
2023-03-08T21:06:50.3350108Z at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
2023-03-08T21:06:50.3350574Z at java.lang.Thread.run(Thread.java:748)
当我在我的电脑上运行同样的代码时,它运行得很好。有没有可能是WebDriver的代码丢失了,所以我才会出现这个错误?
今天浏览了一堆关于这个问题的不同帖子。在大多数情况下,修复方法是在你的驱动程序实例中添加一个chromedriver选项。这就是让我再次工作的原因。
例如:
options.addArguments("--remote-allow-origins=*")
Could not start a new session. Response code 500. unknown error: DevToolsActivePort file doesn't exist. (The process started from chrome location C:\Program Files\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.
-> 但它仍在运行
- Panossa 2023-03-15
相关的Selenium问题是https://github.com/SeleniumHQ/selenium/issues/11750。
如果您升级到使用Java 11+ HTTP客户端,它也将适用于Selenium 4.5+,如https://www.selenium.dev/blog/2022/using-java11-httpclient/中所述。
基本上,将一个依赖关系添加到
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-http-jdk-client</artifactId>
<version>4.8.1</version>
</dependency>
并设置了系统属性
System.setProperty("webdriver.http.factory", "jdk-http-client");
你应该为问题添加以下代码:java.io.IOException。Invalid Status code=403 text=Forbidden and websocket handler
ChromeOptions option = new ChromeOptions();
option.addArguments("--remote-allow-origins=*");
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(option);
这个问题是在更新Chrome到111版本后出现的
解决办法是:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--remote-allow-origins=*","ignore-certificate-errors");
driver = new ChromeDriver(chromeOptions);
我尝试了以上所有的方法,发现都没有效果。然而,我的chrome版本是111.0.5563.64。我试着在chrome 110版本上运行最新版本的chrome驱动,它运行得很好。
或者如果在你的项目中使用了Selenide,只需将Selenide版本更新为 "6.12.2"。这个问题已经被修复了
使用google-chrome v111.0这个错误信息......
2023-03-08T21:06:50.3292859Z Mar 08, 2023 4:06:50 PM org.openqa.selenium.remote.http.WebSocket$Listener onError
2023-03-08T21:06:50.3319163Z WARNING: Invalid Status code=403 text=Forbidden
...是devtools_http_handler
拒绝来自http://localhost:xxxxx 来源的传入WebSocket连接的结果。
快捷的解决方案
正如ChromeDriver verbose log所提示的那样。
[32332:259:0214/190812.204658:ERROR:devtools_http_handler.cc(766)] Rejected an incoming WebSocket connection from the http://localhost:58642 origin. Use the command line flag --remote-allow-origins=http://localhost:58642 to allow connections from this origin or --remote-allow-origins=* to allow all origins.
快速解决这个问题的方法是添加参数--remote-allow-origins=*
,如下所示。
Java:
ChromeOptions options = new ChromeOptions(); options.addArguments("--remote-allow-origins=*"); WebDriver driver = new ChromeDriver(options);