Selenium2学习-030-WebUI自动化实战实例-028-获取元素位置及大小
2023-09-11 14:18:59 时间
自动化测试过程中,有时需要获取元素的位置、大小,以获取元素的位置,通过 Actions 模拟鼠标,进行相对坐标操作。例如,有些元素定位不方便,或者需要对某一元素相对区域范围进行暴力点击测试,此时就需要获取相应的坐标。
获取元素位置和大小的源码比较简单,直接上码了......敬请参阅!
1 /** 2 * Get element position, and return integer Array [left, top, width, height] 3 * 4 * @author Aaron.ffp 5 * @version V1.0.0: autoSeleniumDemo main.aaron.sele.core SeleniumCore.java getElementPositionAndSize, 2015-7-27 23:38:31 Exp $ 6 * 7 * @param webdriver : WebDriver 8 * @param by : By 9 * 10 * @return int[left,top,width,height] 11 */ 12 public int[] getElementPositionAndSize(WebDriver webdriver, By by){ 13 // store element position 14 int[] elementPosition = new int[4]; 15 16 // get element 17 WebElement element = webdriver.findElement(by); 18 19 // get location of element 20 Point e_location = element.getLocation(); 21 22 // get size of element 23 Dimension e_size = element.getSize(); 24 25 elementPosition[0] = e_location.getX(); 26 elementPosition[1] = e_location.getY(); 27 elementPosition[2] = e_size.width; 28 elementPosition[3] = e_size.height; 29 30 return elementPosition; 31 } 32 33 /** 34 * Get element position, and return integer Array [left, top, width, height] 35 * 36 * @author Aaron.ffp 37 * @version V1.0.0: autoSeleniumDemo main.aaron.sele.core SeleniumCore.java getElementPositionAndSize, 2015-7-27 23:35:31 Exp $ 38 * 39 * @param by : By 40 * 41 * @return int[left,top,width,height] 42 */ 43 public int[] getElementPositionAndSize(By by){ 44 // store element position 45 int[] elementPosition = new int[4]; 46 47 // get element 48 WebElement element = this.webdriver.findElement(by); 49 50 // get location of element 51 Point e_location = element.getLocation(); 52 53 // get size of element 54 Dimension e_size = element.getSize(); 55 56 elementPosition[0] = e_location.getX(); 57 elementPosition[1] = e_location.getY(); 58 elementPosition[2] = e_size.width; 59 elementPosition[3] = e_size.height; 60 61 return elementPosition; 62 }
至此,WebUI 自动化功能测试脚本第 028-获取元素位置及大小 顺利完结,希望此文能够给初学 Selenium 的您一份参考。
最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^
相关文章
- 如何取得ChipmunkConstraint实例对象的私有属性
- 【机器学习】朴素贝叶斯应用实例
- spring学习笔记(18)使用JNDI模拟访问应用服务器多数据源实例
- 【ASM学习】在windows 环境下创建ASM实例
- 【ASM学习】普通数据库向ASM实例的迁移(一)
- 线程间通讯:WaitHandler使用实例及分析
- 用python实现简单EXCEL数据统计的实例
- C++设计模式5--单例模式Singleton--当前对象只有一个实例
- Python正则表达式如何进行字符串替换实例
- 【学习总结】Python-3-风格各异的数值类型实例
- 蒙特卡罗仿真(1):入门求生指南(Python实例)
- 【PyTorch】教程:对抗学习实例生成
- 深入学习SAP UI5框架代码系列之五:SAP UI5控件的实例数据修改和读取逻辑
- ZZNUOJ_C语言1083:数值统计(多实例测试)(完整代码)
- 前端MVC Vue2学习总结(二)——Vue的实例、生命周期与Vue脚手架(vue-cli)
- 每天一个JavaScript实例-推断图片是否载入完毕
- WPF – MVVM: how to draw a movable Rectangle with mouse(根据不同的实例画不同的图形)
- UICC 之 USIM 详解全系列——常用命令详解与实例说明
- 【数据库管理】②实例管理及数据库启动关闭
- MVC模式下 provider: SQL Network Interfaces, error: 50 - 发生了 Local Database Runtime 错误。指定的 LocalDB 实例不存在