selenium 学习(6)-元素定位/期望值表达式
对元素定位方法的补充=======================
定位页面元素的基本语法: locatorType=location (某些情况下localType是可以省略的)
默认定义,以下方式可以默认定位:
document ==》Dom 定位策略
// ==》Xpath定位策略
无 ==》identifer 定位策略
1.identifer
第一个使用指定id的页面元素将被识别;
如果没有,识别出第一个名字与指定条件相对的元素
2.id 定位
3.Name定位
4.Xpath定位
Xpath是在xml中定位元素的方法,而html可以被看作xml的一种实现
〇 xpath=//img[@alt=?The image alt text?] 〇 xpath=//table[@id=?table1?]//tr[4]/td[2] 〇 xpath=//a[contains(@href,?#id1?)] 〇 xpath=//a[contains(@href,?#id1?)]/@class 〇 xpath=(//table[@class=?stylee?])//th[text()=?theHeaderText?]/../td 〇 xpath=//input[@name=?name2?and @value=?yes?] 〇 xpath=//*[text()=″right″] |
这里我们常使用contains,另外还有可以使用的
?? starts-with()
?? ends-with()
?? ends-with()
For
example, if you wanted to access the element that has the text "This
element has an ID that changes every time the page is loaded" in it,
youwill use //div[contains(@id,'time_')].
This is due to
the first part of the ID always being static. The locator could also use
starts-with instead of contains to make theXPath query stricter in what
is returned. The queries in the next screenshot will find the same
element on the page:
5.链接文字定位超链接
6.Dom定位
Dom被用来描述Html文档,可使用javascript来访问
〇 dom=document.forms[?myForm?].myDropdown 〇 dom=document.images[56] 〇 dom=function foo() { return document.links[1];}; foo(); |
7.CSS定位
css用来描述html和xml文档的表现
〇 css=a[href=″#id3″] 〇 css=span#firstChid + span |
-------------------------------
总体看,“identifer,id,name,链接文字”方法,不依赖UI元素在页面上的位置
Xpath方法,在使用相对位置表达时,小改变不会影响UI元素定位
用经验的用户会使用CSS,因为css定位要比xpath定位快,且可以找到服务的对象
对文字范本的学习(就是期望值的表达式的写法)=======================以下内容在系列8中做了更详细的实例补充
在阅读了《零成本实现Web自动化测试——基于Selenium和Bromine 》后,学习一下作者的总结
1. globbing 范本
它只有两个 * []
例如* 可以代码任意字符/数字
[0-9]表示数字
[a-zA-Z0-9] 任意字母或数字
实例 glob:*File*Television*
2.Regular experssions 范本(正则表达式)
它有两种 regexp
regexpi (忽略大小写)
支持javascript的特殊字符
实例:用来匹配yahoo的日出时间 regexp:Sunrise:*[0-9]{1,2}:[0-9]{2}[ap]m
3. exact范本
实例:exact:Real *
==================
学习资料来自于《零成本实现Web自动化测试——基于Selenium和Bromine 》以及网络
另外,刚才又看到一个selenium的书:《Selenium测试实践——基于电子商务平台》
TAG: