Selenium之元素进阶操作--鼠标左击,右击,双击,悬停(Action)的应用
来源:互联网 发布:上海行知小学 编辑:程序博客网 时间:2024/05/16 04:45
常见的点击方法集锦:
参数: 1.driver是我们的浏览器 2.Actions是我们系统内置的执行鼠标一系列操作的对象鼠标左击:Actions actions=new Actions(driver); actions.click(这里传入我们需要点击的控件元素,WebElement类型).perform(); //例如: //找到我们慕课网首页登录的id WebElement login=driver.findElement(By.id("js-signin-btn")); //创建动作对象,并且指定操作的浏览器 Actions actions=new Actions(driver); //单击操作传入参数,并且使用perform()提交,生效 actions.click(login).perform();鼠标右击:Actions actions=new Actions(driver); actions.contextClick(这里传入我们需要点击的控件元素,WebElement类型).perform();//鼠标双击:鼠标双击的效果跟我们实际自己点击两次的效果有些出入,也不是那么智能 Actions actions=new Actions(driver); actions.doubleClick(login).perform();鼠标悬停: Actions actions=new Actions(driver); actions.moveToElement(这里传入我们需要点击的控件元素,WebElement类型).perform();前三种操作比较基础就不多做介绍,主要讲一下第四种悬停操作容易犯得错误
鼠标悬停实战
eg:
整体的操作流程: 鼠标指针悬停在"前端开发"一栏,然后点击该栏目中的"HTML/CSS"即可
html“id”导航
代码:
package com.wushuai.test;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;public class ActionTest { public WebDriver driver; //初始化chrome浏览器驱动,并且让chrome浏览器打开慕课网首页 public void initDriver() { System.setProperty("webdriver.chrome.driver", "D:\\java\\chromedriver-32\\chromedriver.exe"); //初始化chrome浏览器 driver=new ChromeDriver(); driver.get("http://www.imooc.com/"); //屏幕最大化 driver.manage().window().maximize(); } /* * 使用Action执行鼠标左击,右击,双击,悬停操作 * * */ public void actionTest() { //找到我们慕课网首页登录的id WebElement login=driver.findElement(By.id("a")); //创建动作对象,并且指定操作的浏览器 Actions actions=new Actions(driver); //单击操作传入参数,并且使用perform()提交,生效 //actions.click(login).perform(); //双击操作 //actions.doubleClick(login).perform(); //右击操作 //actions.contextClick(login).perform(); //actions.moveToElement().perform(); //鼠标悬停操作 actions.moveToElement(login).perform(); } public static void main(String[] args) { // TODO Auto-generated method stub ActionTest at=new ActionTest(); at.initDriver(); at.actionTest(); }}
报错:
no such element: Unable to locate element: {"method":"id","selector":"a"}不能定位元素,也就是说定位元素失败,就是说我们找寻的id为“a”的可能不止一种,系统不能准确定位所以我们可以采用层级定位方法,就是先定位找到父元素,再借助父元素再来找子元素
正确代码:
package com.wushuai.test;import java.util.List;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;public class ActionTest { public WebDriver driver; //初始化chrome浏览器驱动,并且让chrome浏览器打开慕课网首页 public void initDriver() { System.setProperty("webdriver.chrome.driver", "D:\\java\\chromedriver-32\\chromedriver.exe"); //初始化chrome浏览器 driver=new ChromeDriver(); driver.get("http://www.imooc.com/"); //屏幕最大化 driver.manage().window().maximize(); } /* * 使用Action执行鼠标左击,右击,双击,悬停操作 * * */ public void actionTest() { //找到我们慕课网首页包含前段开发,后端开发,移动开发等着一系列条目的className WebElement login=driver.findElement(By.className("menuContent")); //List集合接收前段开发,后端开发,移动开发的条目,注意用的是findElements不是findElement,多一个"s" List<WebElement> elements=login.findElements(By.className("item")); //创建动作对象,并且指定操作的浏览器 Actions actions=new Actions(driver); //单击操作传入参数,并且使用perform()提交,生效 //actions.click(login).perform(); //双击操作 //actions.doubleClick(login).perform(); //右击操作 //actions.contextClick(login).perform(); //actions.moveToElement().perform(); //鼠标悬停在List集合的第一个元素即"前段开发" actions.moveToElement(elements.get(0)).perform(); //点击"前段开发"中包含的"HTML/CSS"条目 driver.findElement(By.partialLinkText("HTML/CSS")).click(); } public static void main(String[] args) { // TODO Auto-generated method stub ActionTest at=new ActionTest(); at.initDriver(); at.actionTest(); }}
总结:
1.鼠标双击使用Actions操作与我们实际操作有出入2.要充分利用父元素与子元素的层级关系,理清关系,防止报错:no such element: Unable to locate element
阅读全文
0 0
- Selenium之元素进阶操作--鼠标左击,右击,双击,悬停(Action)的应用
- Selenium学习8--截图,拖拽页面元素,键盘操作,鼠标右键,悬停,鼠标双击
- Python selenium的webdriver之鼠标悬停
- 鼠标的左击、中击、右击事件及Frame的关闭
- Swing鼠标/左击/右击/中击+事件
- UI TA: selenium webdriver + junit 鼠标悬停,出现另一个元素,点击这个元素的解决方法
- UI TA: selenium webdriver + junit 鼠标悬停,出现另一个元素,点击这个元素的解决方法
- selenium 鼠标悬停事件
- jointJS(四)--关于node的左击与右击
- ArcEngine中实现axMapControl控件鼠标左击右击响应不同事件
- 右击/左击treeview,获取树节点
- selenium中窗口切换和鼠标悬停的例子
- 鼠标悬停,出现另一个元素,点击这个元素的解决方法
- 鼠标悬停,出现另一个元素,点击这个元素的解决方法
- selenium 实现鼠标移动到元素上,并操作元素(鼠标放上后才出现的元素)点击
- 鼠标悬停之放大图片的效果
- 解决WPF右键菜单的菜单项左击有效右击无效的问题
- javascript如何通过鼠标左击获得当前元素ID
- css、js笔记
- fastjson转list bean
- Java native关键字
- hadoop集群搭建详解读
- Spring @Transactional (一) 加强版 标签: springexception数据库jdbcjavahibernate 2010-10-21 17:54 38545人阅读 评论(
- Selenium之元素进阶操作--鼠标左击,右击,双击,悬停(Action)的应用
- 第十二节:上传文件
- 让云朵飘,微信小程序animation循环动画
- API实现HDFS的读写数据流JAVA代码及流程详解
- Linux Shell脚本编程--curl命令详解
- Android PopupWindow使用方法小结
- GraphicsMagick+im4java 图片处理
- iOS ProtocolBuffer 最新操作记录以及“'GPBProtocolBuffers_RuntimeSupport.h' file not found”等问题解决
- OpenCV Shi-Tomasi角点检测以及goodFeatureToTrack()源码分析