java - chromedriver 未在 Java 中实例化

标签 java eclipse selenium selenium-webdriver selenium-chromedriver

我正在使用以下代码尝试创建一个使用 Selenium 的 Java 程序(在 Eclipse Neon 中)。我尝试了一大堆不同的反斜杠和正斜杠组合,例如“C://Users/talai/...”和“C:\Users\talai”等,但没有任何效果。

下面是我的代码:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

import java.awt.Dimension;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.Point;

public class Run {

    public static void main(String[] args) throws InterruptedException {
    for(int d=0; d<20; d++)//run program d<___ times
    {
        //One "Play" session = 5m 15s
        //One "Collect" session = ???
        System.out.println("Session " +d+ " of 300 starting...");
        System.setProperty("webdriver.chrome.driver", "C://Users/talai/Desktop/code/ChipCollector/chromedriver.exe");
        WebDriver driver = new ChromeDriver();

这是输出:

Session 0 of 300 starting...
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function
    at myVegas.Run.main(Run.java:44)
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

最佳答案

几句话:

  • 根据最佳实践,在没有任何适当的情况下,您不应一次打开 20 ChromeDriverChrome 浏览器 实例>用例以及适当硬件的可用性。所以你需要删除for()循环如下:

    for(int d=0; d<20; d++)
    
  • Java 中的系统路径通过双反斜杠 ( \\ ) 或单正斜杠 ( / ) 来表示。两者的工作原理相同。

  • 您看到的错误说明了一切:

     Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function
         at myVegas.Run.main(Run.java:44)
    Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
    

该错误意味着引发了java.lang.NoClassDefFoundError,这是由java.lang.ClassNotFoundException引起的。

<小时/>

什么是 NoClassDefFoundError

<强> NoClassDefFoundError Java 中的 发生在 Java Virtual Machine 无法在运行时找到编译时可用的特定类。例如,如果我们已经解析了来自类的方法调用或访问类的任何静态成员,并且该类在运行时不可用,则JVM 会抛出 NoClassDefFoundError

这清楚地表明Selenium正在尝试在运行时从import org.openqa.selenium.chrome.ChromeDriver;解析ChromeDriver类不再可用。

<小时/>

出了什么问题:

从上述几点可以明显看出,相关的ClassMethods已从一个来源解决 Compile Time Run Time期间不可用.

如果存在多个源来解析Classes,就会出现这种情况。和Methods如:

  • 存在 selenium-server-standalone-3.9.1 JAR 和来自 selenium-java-3.9.1< 的 JAR/strong> 在您的项目空间内。
  • 存在 selenium-server-standalone-3.9.1 JARMaven 相关 JAR
  • 存在 selenium-server-standalone-3.9.1 JARGradle 相关 JAR
<小时/>

解决方案:

这里有几个步骤可以解决NoClassDefFoundError :

  • 使用 selenium-server-standalone-3.9.1 JARselenium-java-3.9.1< 中的 JAR/strong>
  • 如果使用构建工具,例如MavenGradle,从 Java 构建路径中删除所有外部 JARMavenGradle 将下载并解析所有必需的依赖项。
  • 如果在 Java 项目中使用Selenium JAR,则仅在 Java 构建路径中添加所需的外部 JAR,并且删除未使用的。
  • 使用 Maven 时,可以使用 <artifactId>selenium-java</artifactId> <artifactId>selenium-server</artifactId> 。避免同时使用两者。
  • 删除不需要的其他 <dependency> 来自 pom.xml
  • 定期清理 IDE 中的项目工作区,以便构建具有所需依赖项的项目。
  • 使用CCleaner定期清除操作系统杂务的工具。
  • 当您执行 Maven 项目时,始终执行 maven clean , maven install 然后 maven test
  • 如果您的网络浏览器基础版本太旧,请通过Revo Uninstaller卸载网络浏览器并安装最新的 GA 发布版本的网络浏览器

关于java - chromedriver 未在 Java 中实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48959837/

相关文章:

java - 获取本地应用数据目录路径的跨平台方式是什么?

java - 第一个 do-while 循环无法正常运行

eclipse - 需要帮助理解 Eclipse 中的 Git 图标

javascript - 如何检查selenium WDS Jmeter中的div显示属性

java - 堆栈数据结构中索引越界异常

java - eclipse 中的 Junit 在使用 junit 时产生 NoClassDefFoundError

android - 在没有eclipse的情况下安装Android模拟器

android - ADT/Eclipse - main.xml 布局编辑器中的图形/显示 Artifact 和移动文本行

java - 如何提取github登录页面上的错误消息 'Incorrect username or password'

python - 在 gitlab CI/CD 中使用 selenium