【问题标题】:How to import selenium into eclipse?如何将硒导入eclipse?
【发布时间】:2020-08-22 15:27:35
【问题描述】:

我之前创建了一个项目并将其导入 Eclipse,但我还完全重新安装了我的 Windows 并保存了该特定程序。 在那个程序中,我使用的是 Selenium 和 Chrome Webdriver。我安装了它们并将项目添加到我的库中,但是 Eclipse 不允许我导入 Selenium,因此它会引发许多异常。我也导入了一个列表,但那个类似乎工作......

我试过这样导入:

import org.openqa.selenium.*;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;

但是,Eclipse 说:“无法解析导入 org.openqa”

为什么会发生这种情况,我该如何解决?

【问题讨论】:

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


    【解决方案1】:

    这里的问题是您的类路径中没有 org.openqa.selenium 库,因此 eclipse 不知道在哪里可以找到它并为代码完成和突出显示等功能加载内容。

    要添加库 jar 编辑项目设置:Project => Properties => Java Build Path => Libraries => Add JAR ...

    您可以阅读更多here

    【讨论】:

      【解决方案2】:

      首先,如果你使用的是 maven 或 gradle,则无需在 Build path 中显式添加 jar 文件。与您的计划相比,这是一种可持续的解决方案,因为

      1. 如果导入,您的项目将难以在不同的 mmachine 上构建。
      2. 一旦项目变得越来越复杂,就很难跟踪导入的 jar,这就是为什么要使用 maven 或 gradle 等项目管理工具。

      创建一个新的 Maven 项目并在下面添加

         <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
      <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-java</artifactId>
          <version>3.141.59</version>
      </dependency>
      

      如果你是 maven 新手,你可以参考下面的教程来了解如何创建一个 maven 项目,然后请参考下面的教程。它可以解释如何创建项目并添加依赖项。

      https://www.youtube.com/watch?v=YyB2NGV69xE

      【讨论】:

        【解决方案3】:

        看起来你的类路径中没有 selenium jar。 如果您使用的是 maven 项目,则可以在 pom 中添加此依赖项

            <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        

        如果它是一个非 Maven 项目,从这里下载库 https://www.selenium.dev/downloads/ 并设置类路径

         Project => Properties => Java Build Path => Libraries => Add JAR/Add External JAR
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-08-10
          • 2016-02-22
          • 1970-01-01
          • 1970-01-01
          • 2015-09-15
          • 2021-11-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多