查找元素的方法.doc

上传人:飞****9 文档编号:137860602 上传时间:2020-07-12 格式:DOC 页数:4 大小:189KB
返回 下载 相关 举报
查找元素的方法.doc_第1页
第1页 / 共4页
查找元素的方法.doc_第2页
第2页 / 共4页
查找元素的方法.doc_第3页
第3页 / 共4页
查找元素的方法.doc_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《查找元素的方法.doc》由会员分享,可在线阅读,更多相关《查找元素的方法.doc(4页珍藏版)》请在金锄头文库上搜索。

1、1、工具选择:在我们开发测试脚本的过程中各个浏览器给我们也提供了方便定位元素的工具,我比较喜欢使用firefox的firebug工具,也是目前很多开发测试人 员比较热衷的选择,原因是firefox是唯一能够集成selenium IDE的浏览器,并且firebug给用户提供了丰富的扩展组件,我们可以根据自己的需要来选择,一般情况下,使用firebug+firefinder 就足够使用了,firefinder支持xpath以及css选择器定位元素的功能,很方便帮助我们调试测试脚本 2、元素定位的方法:findElement() 与 findElements() findElement() 该方法返

2、回基于指定查询条件的webElement对象,或抛出不符合条件的异常 eg:driver.findElement(By.id(userID); findElements() 该方法返回指定查询条件的WebElement的对象集合,或返回null 3、WebElement对象提供的各种定位元素策略 ID:driver.findElement(By.id()Name:driver.findElement(By.name()className:driver.findElement(By.className()tagName:driver.findElement(By.tagName()linkTex

3、t:driver.findElement(By.linkText()partialLinkText:driver.findElement(By.partialLinkText()css:driver.findElement(By.cssSelector()xpath:driver.findElement(By.xpath()4、webelement类提供了诸多方法,在我们开发脚本过程中如何选择最可靠,效率最高的方法,使用id,name是首选,因为他们在html标签中是唯一的,所以是最可靠的 ID定位:driver.findElement(By.id(username) name定位:drive

4、r.findElement(By.name(username) class定位:driver.findElement(By.className(username) 多学一招:WebElement类支持查询子类元素,如果页面中存在重复元素,但在不同div中,我们可以先定位到其父元素,然后定位其子元素,方法如下: WebElement hello = driver.findElement(By.id(div1).findElement(By.lindText(hello); 5、使用WebElements定位多个相似的元素,比如页面中存在五个单选按钮,他们有相同的class属性,值为:myRadi

5、o,我们想对五个按钮循环操作,我们可以把它们全部取出来放到集合中,然后做循环操作,如下: List radios = driver.findElements(By.className(myRadio);for(int i = 0;iinput) 该方法中“div#logininput” 首先通过相对路径定位到id为login的div元素,然后查找其子元素input(绝对路径) 二、使用xpath定位元素,相比cssSelector,xpath是我比较常用的一种定位元素的方式,因为它很方便,缺点是,消耗系统性能 1、使用绝对路径定位元素 driver.findElement(By.xpath(/

6、html/body/div/form/input) 2、使用相对路径定位元素 driver.findElement(By.xpath(/input) 返回查找到的第一个符合条件的元素 3、使用索引定位元素,索引的初始值为1,注意与数组等区分开 driver.findElement(By.xpath(/input2) 返回查找到的第二个符合条件的元素 4、结合属性值来定位元素 driver.findElement(By.xpath(/inputid=username); driver.findElement(By.xpath(/imgalt=flowr); 5、使用逻辑运算符,结合属性值定位元素

7、,and与or driver.findElement(By.xpath(/inputid=username and name=userID); 6、使用属性名来定位元素 driver.findElement(By.xpath(/inputbutton) 7、类似于cssSlector,使用部分属性值匹配元素 starts-with() driver.findElement(By.xpath(/inputstars-with(id,user)ends-with driver.findElement(By.xpath(/inputends-with(id,name)contains() drive

8、r.findElement(By.xpath(/inputcontains(id,ernam)8、使用任意属性值匹配元素 driver.findElement(By.xpath(/input*=username) 9、使用xpath轴来定位元素 这里略了,详见 三、使用innerText定位元素 1、使用cssSelector查找innerText定位元素 driver.findElement(By.cssSelector(spantextContent=新闻); 2、使用xpath的text函数 driver.findElement(By.xpath(/spancontains(text(),hello) 包含匹配 driver.findElement(By.xpath(/spantext()=新闻) 绝对匹配

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 其它中学文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号