(持续补充)使用selenium启动浏览器遇到的问题汇总
使用selenium启动浏览器遇到的问题汇总:
问题一:可以打开浏览器,但浏览器的地址栏一直显示:data; idea控制台报错:java.io.IOException: Invalid Status code=403 text=Forbidden
SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/D:/software/idea/jmeter/apache-jmeter-5.5_src/apache-jmeter-5.5/lib/log4j-slf4j-impl-2.17.2.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/C:/Users/11303/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.17.0/log4j-slf4j-impl-2.17.0.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory] Starting ChromeDriver 125.0.6422.78 (14db42ec38aded3304a3e624a0a038e02956b87e-refs/branch-heads/6422@{#1088}) on port 57184 Only local connections are allowed. Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe. ChromeDriver was started successfully. 5�� 25, 2024 5:37:31 ���� org.openqa.selenium.remote.ProtocolHandshake createSession ��Ϣ: Detected dialect: W3C 5�� 25, 2024 5:37:31 ���� 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) at org.asynchttpclient.netty.handler.AsyncHttpClientHandler.channelRead(AsyncHttpClientHandler.java:78) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436) at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:324) at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:311) at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:432) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)
解决办法:启动浏览器时对浏览器进行设置:options.addArguments("--remote-allow-origins=*");
ChromeOptions options = new ChromeOptions(); options.addArguments("--remote-allow-origins=*"); WebDriver driver = new ChromeDriver(); driver.get("https://www.baidu.com");
问题二:启动selenium失败,提示ImmutableList找不到
原因:项目依赖问题,ImmutableList类默认在google-collections依赖里面,而该依赖已经迁移到guava(在mvnrepository官网可以查询到:google-collections Relocated -> com.google.guava》guava)
解决方法:在本地项目pom.xml添加guava依赖
<dependency> <groupId>com.google.guava<groupId> <artifactId>guava</artifactId> <version>33.1.1-jre</version> </dependency>
问题三:启动selenium报错:
Possible causes are invalid address of the remote server or browser start-up failure
...
java.lang.NoClassDefFoundError:org/apache/commons/exec/Executor
...
原因:jvm虚拟机在执行时找不到特定类而报错,可能原因是本地项目依赖了其他第三方依赖的Executor类,而该类又不是启动selenium所需要的
解决方法:在本地项目pom.xml添加commons-exec依赖
<dependency> <groupId>org.apache.commons<groupId> <artifactId>commons-exec</artifactId> <version>1.4.0</version> </dependency>
浙公网安备 33010602011771号