1.简介

  有些测试场景或者事件,Selenium根本就没有直接提供方法去操作,而且也不可能把各种测试场景都全面覆盖提供方法去操作。比如:就像鼠标悬停,一般测试场景鼠标悬停分两种常见,一种是鼠标悬停在某一个元素上方,然后会出现下拉子菜单,第二种就是在搜索输入过程,选择自动补全的字段。关于鼠标悬停,selenium把这个方法放在了Actions.java文件中,先来看看鼠标悬停出现下拉菜单的情况。

2.鼠标悬停出现下拉菜单

  鼠标悬停出现下拉菜单,顾名思义就是:鼠标悬停在某一元素上出现下拉菜单。

2.1项目实战

  宏哥这里用百度首页的更多元素,悬停出现拉来菜单,然后点击“音乐”为例进行实战。

2.2代码设计

  代码设计如下:

2.3参考代码

  参考代码如下:

package lessons;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait; /**
* @author 北京-宏哥
*
* 《手把手教你》系列技巧篇(二十九)-java+ selenium自动化测试- Actions的相关操作上篇(详解教程)
*
* 2021年9月26日
*/
public class ActionMusic { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.gecko.driver",".\\Tools\\chromedriver.exe"); // 指定驱动路径 WebDriver driver = new ChromeDriver();
// 最大化窗口
driver.manage().window().maximize();
// 打开百度首页
driver.get("http://wwww.baidu.com");
// 声明一个Action对象
Actions action = new Actions(driver);
// 鼠标移动到 更多产品 上
action.moveToElement(driver.findElement(By.xpath("//a[text()='更多']")))
.perform();
// 显示等待时间10s 等 全部产品>> 出现
WebDriverWait w = new WebDriverWait(driver, 10);
w.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By
.xpath("//a[text()='查看全部百度产品 >']")));
// 等待的元素出现后点击 音乐
WebElement cp = driver.findElement(By.xpath("//a/div[text()='音乐']"));
cp.click();
}
}

2.4运行代码

1.运行代码,右键Run AS->java Application,控制台输出,如下图所示:

2.运行代码后电脑端的浏览器的动作,如下小视频所示:

3.搜索输入过程,选择自动补全的字段

  在搜索框输入关键词后,后提示相关内容,然后将其补全进行搜索。

3.1项目实战

  宏哥这里就以百度搜索为例,进行实战。

3.2代码设计

  代码设计如下:

3.3参考代码

  参考代码如下:

package lessons;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions; /**
* @author 北京-宏哥
*
* 《手把手教你》系列技巧篇(二十九)-java+ selenium自动化测试- Actions的相关操作上篇(详解教程)
*
* 2021年9月26日
*/
public class SearchOpration { public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver",".\\Tools\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://www.baidu.com/"); Thread.sleep(1000); // 设置
WebElement inputbox = driver.findElement(By.id("kw"));
inputbox.sendKeys("selenium a"); // 自动补全其中一个选择项
WebElement auto_text = driver.findElement(By.xpath("//*[@id='form']/div/ul/li[@data-key='selenium appium']")); Actions action = new Actions(driver);
action.moveToElement(auto_text).click().perform();
}
}

3.4运行代码

1.运行代码,右键Run AS->java Application,控制台输出,如下图所示:

2.运行代码后电脑端的浏览器的动作,如下小视频所示:

4.小结

  好了时间也不早了,Actions类中鼠标悬停方法就介绍到这里。提前祝大家、小伙伴和童鞋们国庆节快乐!!!

最新文章

  1. Django URL name详解
  2. C# 属性控件2
  3. testng xml中按顺序执行java类
  4. HTTP响应头和请求头信息对照表
  5. npm使用教程(未完)
  6. Android在API推荐的方式来实现SQLite数据库的增长、删除、变化、检查操作
  7. ubuntu13.10 下一个 g++和gcc 4.8不兼容的问题不能被安装
  8. sharepoint 2010版本 图文安装
  9. linux挂载本地windows分区或目录
  10. 【*】深入理解redis主从复制原理
  11. Iconfont在移动端应用的问题
  12. 2015湖南湘潭 D 二分
  13. 2018.09.23 atcoder Boxes and Candies(贪心)
  14. expect 分发ssh key脚本
  15. 【BZOJ4445】[SCOI2015]小凸想跑步(半平面交)
  16. How to install Jenkins on CentOS 7
  17. Java网络编程(二)关于Socket的一些个人想法
  18. MSSQL 触发器 暂停 和 启动
  19. MVC 枚举 转 SelectListItem
  20. 【剑指offer】面试题 10. 斐波那契数列

热门文章

  1. 通过PEB的Ldr枚举进程内所有已加载的模块
  2. Python如何读写Excel文件-使用xlrd/xlwt模块
  3. Linux centos7 nginx 平滑升级
  4. 剑指offer计划5(查找算法中等版)---java
  5. mybaits源码分析--binding模块(五)
  6. LeetCode通关:通过排序一次秒杀五道题,舒服!
  7. 如何在RHEL7或CentOS 7系统下修改网卡名称(亲测有效~!)
  8. Python - 面向对象编程 - 小实战(3)
  9. Spring5(六)——AspectJ(xml)
  10. 详解JDBC中的Class.forName(DriverName)