与硒有关的日食错误

问题描述 投票:0回答:2

我在使用此代码时遇到一些麻烦。请记住,我对此很新。

package mypackage;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class myclass {
    public static void main(String[] args) {
        System.setProperty("webdriver.gecko.driver", "C:\\Users\\iftikhar\\geckodriver.exe");

        WebDriver driver = new FirefoxDriver();
        String baseUrl = "http://newtours.demoaut.com";
        driver.get(baseUrl);
        String expectedTitle = "Welcome: Mercury Tours";
        String actualTitle = driver.getTitle();

        if (actualTitle.equals(expectedTitle)) {
            System.out.println("Pass");
        } else {
            System.out.println("Fail");
        }

        driver.close();
    }
}

我得到的错误是:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function
    at mypackage.myclass.main(myclass.java:11)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 1 more

有人可以向我解释这个错误并帮助我吗?

谢谢

java eclipse selenium
2个回答
0
投票

这看起来像是Guava库的一个问题。如果您使用maven来管理依赖项,请手动设置Guava版本,如下所示:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>22.0</version>
</dependency>

0
投票

错误堆栈跟踪说明了一切:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function
    at mypackage.myclass.main(myclass.java:11)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function

首先,qazxsw poi被抛出,反过来提升了qazxsw poi


ClassNotFoundException何时发生:

NoClassDefFoundError发生在以下情况:

  • 当我们尝试使用ClassNotFoundException方法加载一个类时,java.lang.classNotFoundException文件或类的二进制文件在类路径中不可用。
  • Class.methodName()尝试使用.class方法加载一个类。
  • 在Java中使用类ClassloaderfindSystemClass()方法。
  • 但大多数情况下,loadClass()只有在JVM尝试在运行时加载一个类时才会出现。

什么是ClassLoader

Java中的ClassNotFoundExceptionClassNotFoundException的子类,当ClassNotFoundException尝试加载特定类并且在类路径中找不到请求的类时发生。它是一个java.lang.Exception,你需要明确地提供JVM,同时使用可能通过使用Checked Exception块或使用Exception Handling子句抛出ClassNotFoundException的方法。

什么地方出了错 :

问题陈述是:

try-catch

如果你看一下throws方法的文档,它被定义为:

if (actualTitle.equals(expectedTitle))

解释:

在你的代码块中,你在equals() equals public boolean equals(Object anObject) Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object. Overrides: equals in class Object Parameters: anObject - The object to compare this String against Returns: true if the given object represents a String equivalent to this string, false otherwise 上调用了equals(),它需要一个Object作为参数。但是您提供了String作为参数,其类型为String而不是Object,其定义如下:

actualTitle

解:

简单的解决方案是使用以下任一方法:

  • expectedTitle:public boolean equalsIgnoreCase(String anotherString)
  • String expectedTitle = "Welcome: Mercury Tours"; :public boolean contains(CharSequence s)
  • equalsIgnoreCase:public boolean contentEquals(StringBuffer sb)
© www.soinside.com 2019 - 2024. All rights reserved.