在尝试运行一个简单的Java – Selenium项目时出现错误。

huangapple go评论91阅读模式
英文:

Getting an error when I try to run a simple Java - Selenium project

问题

我尝试运行这个简单的项目。我已经将所有 Selenium JAR 文件应用到了构建路径中,`chromedriver` 文件位于我的桌面上:

```java
package XYZ;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumScript {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver", "/Users/MYNAME/Desktop/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("https://google.com/");
        System.out.print(driver.getTitle());
        driver.quit();
    }
}

但我得到了以下错误:

Juli 20, 2023 11:35:03 AM org.openqa.selenium.remote.service.DriverService$Builder getLogOutput
INFORMATION: 默认情况下不再将驱动程序日志发送到控制台;https://www.selenium.dev/documentation/webdriver/drivers/service/#setting-log-output
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/io/ByteStreams
    at org.openqa.selenium.remote.service.DriverService$Builder.getLogOutput(DriverService.java:471)
    at org.openqa.selenium.chrome.ChromeDriverService$Builder.createDriverService(ChromeDriverService.java:417)
    at org.openqa.selenium.chrome.ChromeDriverService$Builder.createDriverService(ChromeDriverService.java:197)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:507)
    at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:166)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:50)
    at rocketlaunch.SeleniumScript.main(SeleniumScript.java:11)
Caused by: java.lang.ClassNotFoundException: com.google.common.io.ByteStreams
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    ... 7 more

我期望控制台输出页面标题。


<details>
<summary>英文:</summary>

I try to run this simple project. I have all Selenium JAR files applied in the build path and the `chromedriver` file is located on my desktop:

package XYZ;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumScript {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.setProperty(&quot;websdriver.chrome.driver&quot;, &quot;/Users/MYNAME/Desktop/chromedriver&quot;);
    WebDriver driver = new ChromeDriver();
    driver.get(&quot;https://google.com/&quot;);
    System.out.print(driver.getTitle());
    driver.quit();
}

}


And I get the following error:

```lang-none
Juli 20, 2023 11:35:03 AM org.openqa.selenium.remote.service.DriverService$Builder getLogOutput
INFORMATION: Driver logs no longer sent to console by default; https://www.selenium.dev/documentation/webdriver/drivers/service/#setting-log-output
Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: com/google/common/io/ByteStreams
    at org.openqa.selenium.remote.service.DriverService$Builder.getLogOutput(DriverService.java:471)
    at org.openqa.selenium.chrome.ChromeDriverService$Builder.createDriverService(ChromeDriverService.java:417)
    at org.openqa.selenium.chrome.ChromeDriverService$Builder.createDriverService(ChromeDriverService.java:197)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:507)
    at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:166)
    at org.openqa.selenium.chrome.ChromeDriver.&lt;init&gt;(ChromeDriver.java:50)
    at rocketlaunch.SeleniumScript.main(SeleniumScript.java:11)
Caused by: java.lang.ClassNotFoundException: com.google.common.io.ByteStreams
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    ... 7 more

I expect the console to output the page title.

答案1

得分: 1

com.google.common.io.ByteStreams 类是 Guava 库的一部分。您需要将此库添加到您的类路径中。
如果您使用 Maven,您可以添加以下依赖项:

&lt;!-- https://mvnrepository.com/artifact/com.google.guava/guava --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;com.google.guava&lt;/groupId&gt;
    &lt;artifactId&gt;guava&lt;/artifactId&gt;
    &lt;version&gt;32.1.1-jre&lt;/version&gt;
&lt;/dependency&gt;

到您的 POM 中。

如果您不使用 Maven,请下载 JAR 文件并手动将其添加到类路径中。

顺便说一句:请注意 Java 命名约定。包名应为小写。

英文:

The class com.google.common.io.ByteStreams is part of the Guava libraries. You have to add this library to your class path.
If you use Maven, you can add the dependency:

&lt;!-- https://mvnrepository.com/artifact/com.google.guava/guava --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;com.google.guava&lt;/groupId&gt;
    &lt;artifactId&gt;guava&lt;/artifactId&gt;
    &lt;version&gt;32.1.1-jre&lt;/version&gt;
&lt;/dependency&gt;

to your POM.

If you not use Maven, download the JAR file and add it to the class path manually.

BTW: Take care of Java naming conventions. Package names should be lowercase.

huangapple
  • 本文由 发表于 2023年7月20日 17:44:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728608.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定