zoukankan      html  css  js  c++  java
  • Selenium2学习-031-WebUI自动化实战实例-029-JavaScript 在 Selenium 自动化中的应用实例之四(获取元素位置和大小)

    通过 JS 或 JQuery 获取到元素后,通过 offsetLeft、offsetTop、offsetWidth、offsetHeight 即可获得元素的位置和大小,非常的简单,直接上源码了,敬请参阅!

     1     /**
     2      * Get element position by jquery, and return integer Array [left distance, top distance, width distance, height distance]
     3      * 
     4      * @author Aaron.ffp
     5      * @version V1.0.0: autoSeleniumDemo main.aaron.sele.core SeleniumCore.java getElementPositionAndSizeByJQuery, 2015-7-27 15:54:38 Exp $
     6      * 
     7      * @param selector : selector
     8      * 
     9      * @return int[left,top,width,height]
    10      */
    11     public int[] getElementPositionAndSizeByJQuery(String selector){
    12         // store element position
    13         int[] elementPosition = new int[4];
    14         
    15         String jq = "webelement = $('" + selector + "')[0]; " + 
    16                     "return webelement.offsetLeft + ';' + webelement.offsetTop + ';' + " + 
    17                     "       webelement.offsetWidth + ';' + webelement.offsetHeight";
    18         
    19         String[] position = ((JavascriptExecutor)this.webdriver).executeScript(jq).toString().split(";");
    20         
    21         elementPosition[0] = Integer.valueOf(position[0]);
    22         elementPosition[1] = Integer.valueOf(position[1]);
    23         elementPosition[2] = Integer.valueOf(position[2]);
    24         elementPosition[3] = Integer.valueOf(position[3]);
    25         
    26         return elementPosition;
    27     }

     

    至此,WebUI 自动化功能测试脚本第 029-JavaScript 在 Selenium 自动化中的应用实例之四(获取元素位置和大小) 顺利完结,希望此文能够给初学 Selenium 的您一份参考。(PS:JQuery 在 Selenium 中的应用我也一起归类到了 JavaScript,请知悉!)

    最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

     

  • 相关阅读:
    10、Python的while与死循环
    8、 Python的if分支练习题
    7、 Python中的if多重判断
    6、Python的if判断和两重判断
    5、运算符
    4、数据类型:字典
    placeholder 颜色更改
    禁止video在苹果手机上的自动全屏播放
    点击label出发两次点击事件
    instanceof 和 typeof
  • 原文地址:https://www.cnblogs.com/fengpingfan/p/4699790.html
Copyright © 2011-2022 走看看