简易教程---搭建自动化环境Java+selenium+webdriver+eclipse

   之前看了一些教程,但是依旧在配置环境过程中存在一些问题,希望这个能对大家有所帮助。

   首先是已经配置有java环境,并且能运行代码。

下载selenium-java包:https://docs.seleniumhq.org/download/

       下载Java--3.141.59版本

简易教程---搭建自动化环境Java+selenium+webdriver+eclipse

解压后打开文件夹,有如下的jar包

简易教程---搭建自动化环境Java+selenium+webdriver+eclipse

libs文件夹中:

简易教程---搭建自动化环境Java+selenium+webdriver+eclipse

下载合适的浏览器版本,如:Firefox(版本一般在58-68之间,不能是最新版本,安装后切记要取消自动更新!):https://ftp.mozilla.org/pub/firefox/releases/

        匹配版本如:FireFox 62.0.2 (64位en-us版)+geckodriver 0.26+selenium-Java 3.141.59 

                      或 Firefox:68.0(64bit)+ geckodriver.exe: 0.21

                      或 Firefox:61.0(64bit)+ geckodriver.exe: 0.21/0.22/0.24/0.26

       1.下载合适的浏览器驱动程序(https://github.com/mozilla/geckodriver/releases,这里的是v0.26.0版本,其它的按需下载)

简易教程---搭建自动化环境Java+selenium+webdriver+eclipse

       2.复制到浏览器安装目录,并添加该路径到系统环境变量中:

简易教程---搭建自动化环境Java+selenium+webdriver+eclipse

简易教程---搭建自动化环境Java+selenium+webdriver+eclipse

 

    3.在浏览器中安装辅助插件工具Selenium IDE、Katalon、xPath Finder等;如Firefox中添加Selenium IDE:

   用Firefox打开此链接:https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/,点击add to Firefox即可(博主已经添加,所以此处图示为Remove),添加成功后在右上角工具栏可看到,即可点开使用。

简易教程---搭建自动化环境Java+selenium+webdriver+eclipse

打开Eclipse,点击File -> New -> Dynamic Web Project ->输入项目名称:【如:auto】

注:如果Eclipse项目中没有Dynamic Web Project,请按照此教材添加:点击此处跳转

点击项目auto,右键新建文件夹(New -> Folder),输入文件名为lib
然后将最开始下载的selenium-java包中所有的jar包复制到lib文件夹下(如下图所示,其他根据需要添加jar包)

简易教程---搭建自动化环境Java+selenium+webdriver+eclipse

选中复制进来的所有jar包,点击右键 -> Build Path -> add to Library,完成jar包导入

随后在src下新建一个class,测试一下环境

简易教程---搭建自动化环境Java+selenium+webdriver+eclipse

package automation;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.*;

public class Demo1 {
     public static void main(String[] args)
     {
         WebDriver driver = new FirefoxDriver();
         System.setProperty("webdriver.ie.driver", "C:\\Program Files\\Mozilla Firefox\\geckodriver.exe");
         driver.get("https://www.baidu.com/");
     }
}


点击Run即可打开百度

简易教程---搭建自动化环境Java+selenium+webdriver+eclipse

到此搭建就结束了,如果遇见报错,请耐心上网搜索排除错误,切勿乱删重安,导致心态爆炸。