如何将selenium导入eclipse?

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

How to import selenium into eclipse?

问题

以下是您要翻译的内容:

我之前创建了一个项目,并将其导入了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」

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

英文:

I have previously created a project and I have imported it into Eclipse, but I also completely reinstalled my windows and saved that specific program.
In that program, I am using Selenium and Chrome Webdriver. I installed them both and added the project into my library, but Eclipse won't allow me to import Selenium and because of that it is throwing numerous exceptions. I imported a list as well but that class seemed to work...

I have tried to import it like this:

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;

However, Eclipse says: "the import org.openqa cannot be resolved"

Why is this happening and how can I resolve it?

答案1

得分: 1

问题在于您的类路径中没有 org.openqa.selenium 库,因此 Eclipse 不知道从何处查找并加载其内容,以实现代码补全和语法高亮等功能。

要添加该库的 JAR 文件,请编辑项目设置:项目 => 属性 => Java 构建路径 => 库 => 添加 JAR...

您可以在这里阅读更多信息。

英文:

The issue here is that you do not have the org.openqa.selenium library in your classpath, so eclipse does not know where to look to find it and load the contents for for features like code completion and highlighting.

To add the library jar edit the project settings: Project => Properties => Java Build Path => Libraries => Add JAR ...

You can read more here

答案2

得分: 0

看起来你的类路径中没有Selenium的JAR包。如果你在使用Maven项目,你可以在pom.xml文件中添加以下依赖:

<!-- 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/,然后设置类路径:

项目 => 属性 => Java构建路径 => 库 => 添加JAR/添加外部JAR
英文:

looks like you don't have the selenium jar in your class path.
if you are using a maven project you can add this dependency in the pom

    &lt;!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt;
    &lt;artifactId&gt;selenium-java&lt;/artifactId&gt;
    &lt;version&gt;3.141.59&lt;/version&gt;
&lt;/dependency&gt;

and if its a Non-Maven Project Download the library from here https://www.selenium.dev/downloads/ and set up the classpath

 Project =&gt; Properties =&gt; Java Build Path =&gt; Libraries =&gt; Add JAR/Add External JAR

答案3

得分: 0

首先,如果您正在使用Maven或Gradle,就无需在构建路径中显式添加jar文件。与您计划的方法相比,这是一种可持续的解决方案,因为:

  1. 如果导入不同的机器,您的项目将难以构建。
  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

英文:

First, If you are using maven or gradle, there is no need to Add jar file explicitly in Build path. This is sustainable solution compared to what you are planning because

  1. Your Project will hard to build on different mmachine if imported.
  2. Once the project is grown and getting complex it is hard to keep track of imported jar that's why use project management tool like maven or gradle.

create a new Maven Project and add below

   &lt;!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt;
    &lt;artifactId&gt;selenium-java&lt;/artifactId&gt;
    &lt;version&gt;3.141.59&lt;/version&gt;
&lt;/dependency&gt;

If you are new to maven, you can refer below tutorial on how to create a maven project, then please refer below tutorial. it can explain how to create a project and add the dependencies.

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

huangapple
  • 本文由 发表于 2020年8月22日 23:27:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63537894.html
匿名

发表评论

匿名网友

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

确定