【问题标题】:java.lang.Error: Unresolved compilation problems : WebDriver/ChromeDriver cannot be resolved to a type error while executing selenium testsjava.lang.Error:未解决的编译问题:WebDriver/ChromeDriver 在执行 selenium 测试时无法解析为类型错误
【发布时间】:2021-12-26 08:51:09
【问题描述】:

这是我的代码:

package seleniumTutorials;

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

public class BasicsSelenium {

public static void main(String[] args) {
    boolean status;
    status=true;
    boolean newstatus = false;

    System.out.println("My Old status was "+status);
    System.out.println("My new status was "+newstatus);
    System.setProperty("webdriver.chrome.driver", "F:\\Samraj\\MavenAutomation\\Jar Files\\Selenium Java\\chromedriver.exe");
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("--start-maximized");
    WebDriver driver = new ChromeDriver(chromeOptions);
    driver.get("dev.findmyfare.io");
    System.out.println(driver.getTitle());
 }
 }

以下是声明 webdriver 概念后得到的错误消息:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
WebDriver cannot be resolved to a type   ChromeDriver cannot be resolved to a type
    at seleniumTutorials.BasicsSelenium.main(BasicsSelenium.java:13)

注意:我可以执行简单的java程序。

Screenshot of my Eclipse

【问题讨论】:

  • 您似乎缺少 Selenium 项目的一些罐子。您是否在构建路径中添加了 Selenium 并添加了 jar?
  • 同样driver.get("dev.findmyfare.io") 会抛出一个错误。请在网址前使用httphttps
  • 我添加了丢失的 Junit jar,但仍然面临同样的问题。我也将指导网址更改为dev.findmyfare.io
  • 我在使用 Eclipse 时遇到了这个问题,但在使用 NetBeans 时我没有遇到任何问题,我认为这可以帮助您 stackoverflow.com/questions/59361521/…

标签: eclipse selenium selenium-webdriver webdriver selenium-chromedriver


【解决方案1】:

您的代码没有问题。我创建了一个简单的 Selenium 项目并添加了这段代码,我能够毫无问题地运行这段代码。

请注意,在您的外部 jar 中,您需要有以下 jar 文件

  • client-combined-3.11.0.jar
  • client-combined-3.11.0-sources.jar
  • byte-buddy-1.7.9.jar
  • commons-codec-1.10.jar
  • commons-exec-1.3.jar
  • commons-logging-1.2.jar
  • gson-2.8.2.jar
  • guava-23.6-jre.jar
  • httpclient-4.5.3.jar
  • httpcore-4.4.6.jar
  • okio-1.13.0.jar
  • okhttp-3.9.1.jar

编辑 1:同时从 Referenced Libraries 中删除 ChromeDriver

【讨论】:

  • 感谢您的宝贵建议。我签入了我的项目,我确实添加了您上面列出的所有 jar。
  • 使用调试功能。
  • 我使用了调试功能,但我仍然无法解决问题...您能解释一下如何解决这个问题吗?
【解决方案2】:

此错误消息...

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
WebDriver cannot be resolved to a type
ChromeDriver cannot be resolved to a type

...暗示 WebDriverChromeDriver编译时没有解决。

根据您分享的快照,您的主要问题是您的项目空间中存在多个类似的二进制文件,如下所示:

  • 您已将 selenium-server-standalone-3.11.0 作为依赖项包含在内。
  • 此外,您还包含了来自 selenium-java-3.11.0Java 客户端 JAR 作为依赖项。

因此,您很可能已经从一个 JAR 资源(即 selenium-server-standalone-3.11.0selenium-java-3.11.0 JAR),但 compiletime 类正试图从其他 JAR 中解析.因此您会看到 java.lang.Error: Unresolved compiler questions

解决方案

  • 要么只保留 selenium-server-standalone-3.11.0 JAR 作为外部 JAR。
  • 或者只保留 selenium-java-3.11.0 JAR 作为外部 JAR。
  • 删除所有其他 Selenium Java 客户端 JAR
  • 清理你的项目工作区通过你的IDE重建你的项目只需要依赖。
  • 进行系统重启
  • 执行您的@Test

【讨论】:

    【解决方案3】:

    我在使用 IntelliJ IDEA 时遇到了同样的错误,解决方案是更新我的 Gradle 文件“build.gradle”以包含

    dependencies {
    // https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.59'}
    
    test {
    useJUnitPlatform()}
    

    【讨论】:

      【解决方案4】:

      在eclipse中,添加jar文件时,我们有两个选项第一个是modulePath,第二个是classPath, 所以你需要在 classPath 中而不是在 ModulePath 中添加所有必需的 jar 文件 它对我有用

      【讨论】:

        【解决方案5】:

        在较旧的 Eclips 中,我们无法选择在类路径或模块路径下添加 jar。

        现在我们有了一个选项,所以如果你在模块路径中添加了所有有效的 jar,但仍然出现错误,那是因为在模块路径中添加了 jar。

        移除 jars 并添加到 Classpath 下

        附上截图供参考

        Screenshot to fix ChromeDriver cannot be resolved to a type erro

        【讨论】:

          猜你喜欢
          • 2018-10-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-03
          • 2013-02-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多