Java.io.IOException:无效状态代码 = 403 文本 = 禁止

Pet*_*zov 29 selenium-webdriver

当我尝试在远程服务器的 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)
Run Code Online (Sandbox Code Playgroud)

当我从我的电脑上运行相同的代码时,它运行良好。您知道 WebDriver 代码是否可能丢失,因此我收到此错误?

小智 61

浏览今天出现的一系列关于此问题的不同帖子。大多数情况下的解决方法是向您的驱动程序实例添加 chromedriver 选项。这就是让事情再次为我工作的原因。

例子:

options.addArguments("--remote-allow-origins=*")
Run Code Online (Sandbox Code Playgroud)


小智 14

相关的 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>
Run Code Online (Sandbox Code Playgroud)

并设置系统属性

System.setProperty("webdriver.http.factory", "jdk-http-client");
Run Code Online (Sandbox Code Playgroud)


Deb*_*anB 7

使用 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
Run Code Online (Sandbox Code Playgroud)

devtools_http_handler...是拒绝来自 http://localhost:xxxxx 源的传入 WebSocket 连接的结果。


快速解决方案

正如 ChromeDriver详细日志所示:

[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.
Run Code Online (Sandbox Code Playgroud)

解决此问题的一个快速方法是添加参数,--remote-allow-origins=*如下所示:


参考

您可以在以下位置找到一些详细的讨论: