下载Selenium JAR文件并在Eclipse中配置
Selenium提供了多种编程语言的API实现。然而,基于Java的Selenium API绑定是最受欢迎的。在本教程中,我们将学习如何下载Selenium JAR文件并配置Eclipse IDE以在编写测试脚本时使用它们。要将Eclipse配置为使用Selenium WebDriver,我们需要执行以下活动:
-
- 安装Java
-
- 安装Eclipse集成开发环境
- 配置Eclipse集成开发环境与WebDriver
安装 Java
- Step 1: Go to the official website (https://www.oracle.com/technetwork/java/javase/downloads) and click on the Download tab.

- Step 2: Click Icon under Java SE Downloads

- Step 3: Accept the License Agreement and choose the JDK that corresponds to your Operating System.

- Step 4: Once the download is complete, run the executable file to install JDK in Windows. Perform similar steps for Mac DMG file. For Linux, you just need to untar and set PATH variable to execute java commands.

从Java 11开始,Java许可证发生了重大变化。对于商业用途,Oracle JDK不再免费。然而,您可以使用完全免费的OpenJDK在生产环境中使用。了解更多信息,请阅读Java 11功能。
安装Eclipse IDE
让我们来看看如何在Windows上安装Eclipse IDE。对于Mac和Linux操作系统,步骤几乎相似。您需要根据相应操作系统的Eclipse安装文件执行类似的步骤。
- Step 1: Go to the Eclipse official website and click Download button of Eclipse IDE icon.

- Step 2: Once the download is complete, run the exe file to start Eclipse installer application for Windows.

- Step 3: Click “Eclipse IDE for Java Developers” in the installer window.

- Step 4: After that, a new window will open. Change the Installation Folder path to “C:\eclipse” and click on install button.

- Step 5: After successful completion of installation, a new window will open. Click on the Launch button in the new window.

- Step 6: This will launch Eclipse IDE for you. From next time onwards, you can start Eclipse from its installation folder.

下载Selenium JAR文件
基本上,Selenium不需要安装,只需进行配置即可。我们只需要下载JAR包并在Eclipse中导入即可。
- Step 1: Go to the official website and click on Download tab.

- Step 2: Click on Download link to download the jars for selenium.

- Step 3: Extract the downloaded folder and we will get the folder like selenium-java-3.141.59.

使用Selenium WebDriver配置Eclipse IDE
- Step 1: Launch the “eclipse.exe” file inside the eclipse installation folder.
- Step 2: When asked to select for a workspace, just click Launch button to accept the default location.

- Step 3: Create a new Java Project from File > New > Project.

- Step 4: Give your Project name ‘Testing‘ as shown below. Click on Finish button.

- Step 5: Right click on Project name Testing and select New > Package.

- Step 6: Give Package name automation and click on Finish button.

现在您可以在项目名为Testing下看到一个名为自动化的新包裹。

- Step 7: Right click on package automation and select New > Class.

- Step 8: Give Class name Test, check the option checkbox public static void main and click on Finish button.

- Step 9: Now your Eclipse window will look something like below.

- Step 10: Now we add few lines of Selenium code without adding JAR files. Below image shows us the error message when we won’t add jar files.

我们继续并将Selenium JAR文件添加到我们的项目中以解决这些错误。- 步骤11:右键单击项目Testing。选择属性> Java build Path。然后点击“Libraries”选项卡,再点击“Add External JARs”。

- Step 12: Add client-combined jar from the selenium jars folder.

- Step 13: Add all the jars under libs folder.

- Step 14: Click Apply and Close button.

- Step 15: Now your Project Package Explorer window should look similar to the below image. Also, the errors related to Selenium classes would have been disappeared. Here we need to import WebDriver and ChromeDriver classes.

就这样。我们的Eclipse Java项目已配置为使用selenium类。
Selenium Maven依赖
这些天的大多数Java项目都使用Maven作为构建工具。在这种情况下,将Selenium的jar包导入到您的项目中非常容易。只需要将以下依赖项添加到您的项目的pom.xml文件中即可。
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
如果你对Maven项目还不熟悉,以下是需要遵循的步骤。
- Step 1: Open Eclipse then go to File > New > Project.

- Step 2: In the new project window, there are many wizard to choose. Select Maven Project under Maven category and click on Next button.

- Step 3: Click on Next button in the new screen. Don’t forget to select the “Use default Workspace location” option.

- Step 4: Select the maven-archetype-quickstart in the next screen and click on Next button.

- Step 5: Enter Group Id, Artifact Id and click on Finish button.

- Step 6: Once the project is created you can see the maven project in the project explorer.

- Step 7: Double click on pom.xml and you will get the source code of pom.xml in the editor.

- Step 8: We need selenium maven dependencies configuration to add to our project. Head over to https://www.seleniumhq.org/download/maven.jsp. Here Selenium guys have provided maven dependency information. Just copy them as we need to paste it in pom.xml file.

- Step 9: Open pom.xml file and create tag and paste the dependency code inside it. Save the pom.xml file changes.

Selenium Gradle 依赖
Gradle也是一种流行的构建工具,被许多项目使用。如果您正在使用Gradle,则在您的build.gradle文件中添加以下依赖项。
compile "org.seleniumhq.selenium:selenium-java:3.141.59"

上述内容的简要概述。
我们学习了如何配置我们的系统以开始使用Selenium。我们安装了Java、Eclipse,并创建了一个新项目并添加了Selenium的jar文件。然而,对于我们的项目来说,最好使用Maven或Gradle构建工具,我们也学习了如何使用Maven和Gradle脚本添加Selenium的jar文件。参考资料:Java SE下载、Eclipse下载、Selenium jar文件。