Web自动化测试-掌握selenium工具用法,使用WebDriver测试Chrome/FireFox网页(Java
目录
一、在Eclipse中构建Maven项目
1.全局配置Maven
2.配置JDK路径
3.创建Maven项目
4.引入selenium-java依赖
二、Chrome自动化脚本编写
1.创建一个ChromeTest类
2.测试ChromeDriver
3.下载chromedriver驱动
4.在脚本中通过System.setProperty方法指定chromedriver的地址
5.测试学习通网址登录功能
三、FireFox自动化脚本编写
1.新建一个FireFoxTest类
2.指定firefox可执行文件路径: webdriver.firefox.bin
3.下载geckodriver驱动
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
输入以下代码,点击运行
import org.openqa.selenium.chrome.ChromeDriver;public class ChromeTest {public static void main(String[] args) throws Exception{ChromeDriver driver = new ChromeDriver();}
}
若报以下错,说明Selenium 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.0at 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版本
<dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>3.141.59</version>
</dependency>
保存后,再次运行报错以下信息,这是正常情况,因为我们还没有设置Chrome浏览器的驱动
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.htmlat 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浏览器驱动各版本下载(...113、114、115、116、117、118、119、120、121、122、123、124、125、126、127)(原创) - Z哎呀 - 博客园 (cnblogs.com)
将下载解压的chromedriver.exe复制

粘贴到项目的resources里面

4.在脚本中通过System.setProperty方法指定chromedriver的地址
// 系统设置Chrome驱动文件的路径
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
再次运行,此时弹出Chrome窗口,运行成功

5.测试学习通网址登录功能
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类
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安装在其它路径,不是默认的安装路径
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: FirefoxDriverat 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执行文件,添加路径对应以下代码,再次运行
//指定firefox可执行文件路径
System.setProperty("webdriver.firefox.bin","C:\\Users\\86153\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
FirefoxDriver driver = new FirefoxDriver();
再次报错以下信息,这也是因为没有配置FireFox驱动
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/releasesat 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的地址
//指定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窗户,成功!

相关文章:
Web自动化测试-掌握selenium工具用法,使用WebDriver测试Chrome/FireFox网页(Java
目录 一、在Eclipse中构建Maven项目 1.全局配置Maven 2.配置JDK路径 3.创建Maven项目 4.引入selenium-java依赖 二、Chrome自动化脚本编写 1.创建一个ChromeTest类 2.测试ChromeDriver 3.下载chromedriver驱动 4.在脚本中通过System.setProperty方法指定chromedriver的…...
maven多模块项目搭建
文章目录 创建方式创建父项目创建子模块 目录结构示例父模块模块A模块B(并在模块B中引入模块A) 注意事项 创建方式 创建父项目 #创建文件夹后,进入目录,执行以下命令 PS D:\demo> mvn archetype:generate #将输出很多模板&am…...
PostgreSQL的视图pg_tables
PostgreSQL的视图pg_tables pg_tables 是 PostgreSQL 中的一个系统视图,用于显示当前数据库中所有用户定义的表的信息。这个视图提供了关于表的名称、所属模式(schema)、所有者以及表类型等详细信息。 pg_tables 视图的主要列 列名类型描述…...
Stable diffusion采样器详解
在我们使用SD web UI的过程中,有很多采样器可以选择,那么什么是采样器?它们是如何工作的?它们之间有什么区别?你应该使用哪一个?这篇文章将会给你想要的答案。 什么是采样? Stable Diffusion模…...
为什么要进行渗透测试?
渗透测试的重要性 渗透测试是一种安全评估技术,旨在模拟黑客攻击,发现和利用系统漏洞,以评估企业信息系统的安全性。以下是进行渗透测试的几个主要原因: 1.发现潜在的漏洞和安全风险:渗透测试可以模拟真实的攻击行为…...
后方碰撞预警系统技术规范(简化版)
后方碰撞预警系统技术规范(简化版) 1 系统概述2 预警区域3 预警目标4 功能需求功能条件5 显示需求6 指标需求1 系统概述 后方碰撞预警系统RCW(Rear Collision Warning)是在后方车辆即将与自车发生碰撞之前,激活危险警告灯以较高频率闪烁,从而吸引后方驾驶员的注意力,避免…...
Position定位
Position定位 CSS中position属性是比较常用的元素定位方案,position常用的取值有static、relative、absolute、fixed、sticky、inherit。 static static属性是HTML元素的默认值,即没有定位,遵循正常的文档流对象,对于top、bott…...
npm install 的原理
1. 执行命令发生了什么 ? 执行命令后,会将安装相关的依赖,依赖会存放在根目录的node_modules下,默认采用扁平化的方式安装,排序规则为:bin文件夹为第一个,然后是开头系列的文件夹,后…...
基于I2C协议的OLED显示(利用U82G库)
目录 一、I2C协议 1、了解I2C协议的基本原理和时序协议 基本原理 时序协议 2、掌握0.96寸OLED屏的工作原理,汉字点阵显示原理 OLED 工作原理 汉字点阵显示原理 3、掌握开源GUI库U82G在stm32上的移植编译方法,以及图形界面可视化技术。 二、具体…...
【文末附gpt升级秘笈】探索AGI之路:穿越大模型的冰与火,谱写未来技术的乐章
探索AGI之路:穿越大模型的冰与火,谱写未来技术的乐章 摘要 随着人工智能技术的飞速发展,大模型成为了业界关注的焦点。然而,大模型并非万能,其背后隐藏着诸多迷思与挑战。本文基于“AGI技术50人”访谈栏目的素材&…...
国内12寸先进封装厂家的一些情况
一、12寸先进封装厂家 在中国大陆,专注于12英寸(300mm)晶圆的先进封装技术的企业包括但不限于以下几家: 1. 长电科技(JCET Technologies Co., Ltd.):长电科技是中国领先的半导体封装测试企业之…...
【代码随想录训练营】【Day 48】【动态规划-7】| 卡码 57, Leetcode 322, 279
【代码随想录训练营】【Day 48】【动态规划-7】| 卡码 57, Leetcode 322, 279 需强化知识点 python 的幂次计算, 10 ** 5, 10 **(0.5) 题目 卡码 57. 爬楼梯(第八期模拟笔试) 注…...
【Qt】Qt常见的数据类型
思维导图 学习目标 一、基础类型 因为Qt是一个C的框架,因此C的语法和数据类型在Qt中都是被支持的,但是Qt中也是定义了一些属于自己的数据类型,不过,好多数据类型都是对C的数据类型进行封装,下面来简要介绍一下这些基…...
【源码】Spring Data JPA原理解析之事务执行原理
Spring Data JPA系列 1、SpringBoot集成JPA及基本使用 2、Spring Data JPA Criteria查询、部分字段查询 3、Spring Data JPA数据批量插入、批量更新真的用对了吗 4、Spring Data JPA的一对一、LazyInitializationException异常、一对多、多对多操作 5、Spring Data JPA自定…...
第十一篇——信息增量:信息压缩中的保守主义原则
目录 一、背景介绍二、思路&方案三、过程1.思维导图2.文章中经典的句子理解3.学习之后对于投资市场的理解4.通过这篇文章结合我知道的东西我能想到什么? 四、总结五、升华 一、背景介绍 通过信息中的保守主义,我想到了现实中人的保守主义一样&#…...
中国飞行器设计创新大赛多旋翼无人机任务飞行
源码:后续补充 1、启动launch文件 roslaunch robot_bringup mission.launch <launch> <!--启动mavros节点 --><include file"$(find mavros)/launch/px4.launch" /><!--启动USB摄像头节点 --><include file"$(find…...
WPF-UI布局
WPF布局元素有如下几个: Grid:网格。可以自定义行和列并通过行列的数量、行高和列宽来调整控件的布局。StackPanel:栈式面板。可将包含的元素在竖直或水平方向上排成一条直线,当移除一个元素后,后面的元素会自动向前移…...
武忠祥17堂课没必要全听,这几个才是精华!
作者:Captain 链接:https://www.zhihu.com/question/381665751/answer/3197724055 来源:知乎 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 17堂课类似于习题课,是专题训练 17堂课省略了…...
Android 蓝牙profile的配置
在做BQB认证之前,我们需要确认那些profile之前,我们需要查看profile的配置情况 Android13版本前蓝牙profile的配置 MTK的项目代码如下 vendor\mediatek\proprietary\packages\modules\Bluetooth\android\app\res\values\config.xml 高通的项目代码如…...
Selenium时间等待_显示等待
特点: 针对具体元素进行时间等待 可以自定义等待时长和间隔时间 按照设定的时间,不断定位元素,定位到了直接执行下一步操作 如在设定时间内没定位到元素,则报错(TimeOutException) 显示等待概念&#x…...
conda相比python好处
Conda 作为 Python 的环境和包管理工具,相比原生 Python 生态(如 pip 虚拟环境)有许多独特优势,尤其在多项目管理、依赖处理和跨平台兼容性等方面表现更优。以下是 Conda 的核心好处: 一、一站式环境管理:…...
大数据学习栈记——Neo4j的安装与使用
本文介绍图数据库Neofj的安装与使用,操作系统:Ubuntu24.04,Neofj版本:2025.04.0。 Apt安装 Neofj可以进行官网安装:Neo4j Deployment Center - Graph Database & Analytics 我这里安装是添加软件源的方法 最新版…...
C++:std::is_convertible
C++标志库中提供is_convertible,可以测试一种类型是否可以转换为另一只类型: template <class From, class To> struct is_convertible; 使用举例: #include <iostream> #include <string>using namespace std;struct A { }; struct B : A { };int main…...
通过Wrangler CLI在worker中创建数据库和表
官方使用文档:Getting started Cloudflare D1 docs 创建数据库 在命令行中执行完成之后,会在本地和远程创建数据库: npx wranglerlatest d1 create prod-d1-tutorial 在cf中就可以看到数据库: 现在,您的Cloudfla…...
Python实现prophet 理论及参数优化
文章目录 Prophet理论及模型参数介绍Python代码完整实现prophet 添加外部数据进行模型优化 之前初步学习prophet的时候,写过一篇简单实现,后期随着对该模型的深入研究,本次记录涉及到prophet 的公式以及参数调优,从公式可以更直观…...
P3 QT项目----记事本(3.8)
3.8 记事本项目总结 项目源码 1.main.cpp #include "widget.h" #include <QApplication> int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); } 2.widget.cpp #include "widget.h" #include &q…...
现代密码学 | 椭圆曲线密码学—附py代码
Elliptic Curve Cryptography 椭圆曲线密码学(ECC)是一种基于有限域上椭圆曲线数学特性的公钥加密技术。其核心原理涉及椭圆曲线的代数性质、离散对数问题以及有限域上的运算。 椭圆曲线密码学是多种数字签名算法的基础,例如椭圆曲线数字签…...
Go 语言并发编程基础:无缓冲与有缓冲通道
在上一章节中,我们了解了 Channel 的基本用法。本章将重点分析 Go 中通道的两种类型 —— 无缓冲通道与有缓冲通道,它们在并发编程中各具特点和应用场景。 一、通道的基本分类 类型定义形式特点无缓冲通道make(chan T)发送和接收都必须准备好࿰…...
push [特殊字符] present
push 🆚 present 前言present和dismiss特点代码演示 push和pop特点代码演示 前言 在 iOS 开发中,push 和 present 是两种不同的视图控制器切换方式,它们有着显著的区别。 present和dismiss 特点 在当前控制器上方新建视图层级需要手动调用…...
day36-多路IO复用
一、基本概念 (服务器多客户端模型) 定义:单线程或单进程同时监测若干个文件描述符是否可以执行IO操作的能力 作用:应用程序通常需要处理来自多条事件流中的事件,比如我现在用的电脑,需要同时处理键盘鼠标…...
