如何使我的硒代码工作...打开浏览器并输入 google.com

问题描述 投票:0回答:1
import org.openqa.selenium.By;  
import org.openqa.selenium.JavascriptExecutor;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.chrome.ChromeDriver;  
  
public class HelloWorldSelenium {  
  
    public static void main(String[] args) {  
      
           // System Property for Chrome Driver   
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Kamilia\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");  
          
             // Instantiate a ChromeDriver class.     
        WebDriver driver= new ChromeDriver();  
          
           // Launch Website  
        driver.navigate().to("http://www.google.com/");  
          
         //Maximize the browser  
          driver.manage().window().maximize();              
    }    
}  

我只是想打开 chromedriver 并输入 google.com。但我收到错误....chrome 打开但地址中没有写入任何内容...您能帮忙吗

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting ChromeDriver 126.0.6478.126 (d36ace6122e0a59570e258d82441395206d60e1c-refs/branch-heads/6478@{#1591}) on port 57956
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
juin 26, 2024 12:08:43 P.M. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
juin 26, 2024 12:08:43 P.M. org.openqa.selenium.remote.http.WebSocket$Listener onError
WARNING: 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)
    at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
    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.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:1570)

Exception in thread "main" org.openqa.selenium.remote.http.ConnectionFailedException: Unable to establish websocket connection to http://localhost:61022/devtools/browser/bf66aa4a-459d-4e91-b1e4-12698a7dc404
Build info: version: '4.1.0', revision: '87802e897b'
System info: host: 'DESKTOP-BVFDT7U', ip: '192.168.0.37', os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '22'
Driver info: driver.version: ChromeDriver
    at org.openqa.selenium.remote.http.netty.NettyWebSocket.<init>(NettyWebSocket.java:104)
    at org.openqa.selenium.remote.http.netty.NettyWebSocket.lambda$create$3(NettyWebSocket.java:137)
    at org.openqa.selenium.remote.http.netty.NettyClient.openSocket(NettyClient.java:116)
    at org.openqa.selenium.devtools.Connection.<init>(Connection.java:77)
    at org.openqa.selenium.chromium.ChromiumDriver.lambda$new$2(ChromiumDriver.java:124)
    at java.base/java.util.Optional.map(Optional.java:260)
    at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:122)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:104)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:91)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:46)
    at HelloWorldSelenium.main(HelloWorldSelenium.java:14)

更新 chromedriver 在pom.xml文件中添加依赖项

selenium-webdriver selenium-java
1个回答
0
投票

将您的 selenium 升级到最新版本,至少

v4.6.0
或更高版本。如果你这样做了,那么你就不再需要下面的行了,

System.setProperty("webdriver.chrome.driver", "C:\\Users\\Kamilia\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");

您的代码可以简化为:

public static void main(String[] args) {  
             
    WebDriver driver= new ChromeDriver();  
    driver.get("http://www.google.com/");
    driver.manage().window().maximize();               
} 

参考资料 - https://www.selenium.dev/blog/2022/introducing-selenium-manager/

© www.soinside.com 2019 - 2024. All rights reserved.