目录
4.在脚本中通过System.setProperty方法指定chromedriver的地址
[2.指定firefox可执行文件路径: webdriver.firefox.bin](#2.指定firefox可执行文件路径: webdriver.firefox.bin)
4.在脚本中通过System.setProperty方法指定chromedriver的地址
工具:eclipse(2016)、chrome(v.125)
依赖:selenium-java(3.141.59)
驱动:chromedriver(win64 v125)
配置环境:jdk1.8.0、 maven3.5.2
一、在Eclipse中构建Maven项目
1.全局配置Maven
点击Windows->Preferences
注意:要先在settinfs.xml中配置阿里云镜像仓库,可参考该文章1~3步骤IDEA 使用自定义MAVEN(maven安装及IDEA配置)_idea 用自定义maven-CSDN博客
同时在installations中add maven路径
2.配置JDK路径
同样是在Preferences中,确认指向的是JDK的路径而不 是JRE的路径
3.创建Maven项目
点击File->New->Project...
勾选Create a simple...
填入组名和项目名,点击Finish
创建完项目列表如下:
4.引入selenium-java依赖
在Maven官网可以下载:Maven Repository: Search/Browse/Explore (mvnrepository.com)
搜索selenium,选择Selenium Java
选择使用度较高的版本,这里选择了4.18.1
拷贝对应的Maven依赖包
点击pom.xml粘贴进去,注意要放在<**dependencies></dependencies>**里面
保存后,后自动生成Maven Dependendies
二、Chrome自动化脚本编写
1.创建一个ChromeTest类
2.测试ChromeDriver
输入以下代码,点击运行
java
import org.openqa.selenium.chrome.ChromeDriver;
public class ChromeTest {
public static void main(String[] args) throws Exception{
ChromeDriver driver = new ChromeDriver();
}
}
若报以下错,说明Selenium Java版本过高,需要下载较低版本
java
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/openqa/selenium/chrome/ChromeDriver has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.test.ChromeTest.main(ChromeTest.java:9)
这里我将依赖换成了3.141.59版本
java
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
保存后,再次运行报错以下信息,这是正常情况,因为我们还没有设置Chrome浏览器的驱动
java
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
at com.google.common.base.Preconditions.checkState(Preconditions.java:847)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:134)
at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:35)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:159)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:94)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
at com.test.WebTest01.main(WebTest01.java:12)
3.下载chromedriver驱动
因为我的Chrome浏览器的版本是125,在以下链接可以下载对应版本
将下载解压的chromedriver.exe复制
粘贴到项目的resources里面
4.在脚本中通过System.setProperty方法指定chromedriver的地址
java
// 系统设置Chrome驱动文件的路径
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
再次运行,此时弹出Chrome窗口,运行成功
5.测试学习通网址登录功能
java
public static void main(String[] args) throws Exception{
// 系统设置Chrome驱动文件的路径
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
//最大化浏览器窗口
driver.manage().window().maximize();
// 测试学习通网站
String url = "https://passport2.chaoxing.com";
driver.get(url);
Thread.sleep(3000);
// 通过F12查看对应id
// 输入手机号
WebElement phoneElement = driver.findElement(By.id("phone")); //手机号
phoneElement.clear(); //清空文本输入框中的内容
phoneElement.sendKeys("xxxxxxxxxx"); //在文本输入框中输入内容
String phoneValue = phoneElement.getAttribute("value"); //获取文本框中已经输入的内容
Thread.sleep(1000);
// 输入密码
WebElement passwordElement = driver.findElement(By.id("pwd"));
passwordElement.clear();
passwordElement.sendKeys("xxxxxxxxxx");
String passwordValue = passwordElement.getAttribute("value");
Thread.sleep(1000);
//输出对应值
System.out.println(phoneValue);
System.out.println(passwordValue);
//通过className获取自动勾选框
WebElement checkElement = driver.findElement(By.className("check-input"));
// 使用isSelected()方法检查复选框是否被选中
boolean isSelected = checkElement.isSelected();
// 输出结果
System.out.println("复选框是否被选中: " + isSelected);
// 如单复选框没有被选中
if (!isSelected) {
checkElement.click(); //点击选中
}
Thread.sleep(3000);
// 点击登录
WebElement loginElement = driver.findElement(By.id("loginBtn"));
loginElement.click();
}
点击运行后,将会弹出网址,自动登录
三、FireFox自动化脚本编写
1.新建一个FireFoxTest类
java
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FireFoxTest {
public static void main(String[] args) throws Exception{
FirefoxDriver driver = new FirefoxDriver();
}
}
运行后报错以下信息,这是正常情况,原因是firefox安装在其它路径,不是默认的安装路径
java
Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: WIN10
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'DESKTOP-VMB9KRO', ip: '10.194.105.24', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_121'
Driver info: driver.version: FirefoxDriver
at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:100)
at java.util.Optional.orElseGet(Optional.java:267)
at org.openqa.selenium.firefox.FirefoxOptions.getBinary(FirefoxOptions.java:216)
at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:187)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:147)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:125)
at com.test.FireFoxTest.main(FireFoxTest.java:10)
2.指定firefox可执行文件路径: webdriver.firefox.bin
找到Firefox的exe执行文件,添加路径对应以下代码,再次运行
java
//指定firefox可执行文件路径
System.setProperty("webdriver.firefox.bin","C:\\Users\\86153\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
FirefoxDriver driver = new FirefoxDriver();
再次报错以下信息,这也是因为没有配置FireFox驱动
java
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
at com.google.common.base.Preconditions.checkState(Preconditions.java:847)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:134)
at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:44)
at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:167)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355)
at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:190)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:147)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:125)
at com.test.FireFoxTest.main(FireFoxTest.java:11)
3.下载geckodriver驱动
selenium,geckodriver,firefox对应版本官网参考:
Supported platforms --- Firefox Source Docs documentation (mozilla.org)
我的FireFox版本是126,下载对应0.34.0版本
进入网址:https://github.com/mozilla/geckodriver ,点击"Tags",点击0.34.0版本的download,然后选择对应平台的压缩包下载
同理,解压复制到resources文件里
4.在脚本中通过System.setProperty方法指定chromedriver的地址
java
//指定firefox可执行文件路径
System.setProperty("webdriver.firefox.bin","C:\\Users\\86153\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
// 系统设置gecko驱动文件的路径
System.setProperty("webdriver.gecko.driver", "src/test/resources/geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver();
再次运行,弹出Firefox窗户,成功!