Selenium CSS页面元素定位-自动化测试
更新时间:2022-04-19 09:30:36 作者:多测师 浏览:228
CSS 可以比较灵活选择控件的任意属性,一般情况下定位速度要比 XPath 快,但对于初学者来说比较难以学习使用,下面我们就详细的介绍 CSS的语法与使用。
<html>
<body>
<div class="formdiv">
<form name="fnfn">
<input name="username" type="text"></input>
<input name="password" type="text"></input>
<input name="continue" type="button"></input>
<input name="cancel" type="button"></input>
<input value="SYS123456" name="vid" type="text">
<input value="ks10cf6d6" name="cid" type="text">
</form>
<div class="subdiv">
<ul id="recordlist">
<p>Heading</p>
<li>Cat</li>
<li>Dog</li>
<li>Car</li>
<li>Goat</li>
</ul>
</div>
</div>
</body>
</html>
css中的结构性定位
结构性定位就是根据元素的父子、同级中位置来定位,css3标准中有定义一些结构性定位伪类如nth-of-type,nth-child,但是使用起来语法很不好理解,这里就简单介绍下。Selenium中则是采用了来自 Sizzle的 css3定位扩展,它的语法更加灵活易懂。
nth-of-type
例:
#test > li:nth-of-type(2) //表示ID为test下的第二个li元素,相当于xpath= //e[id=test]/li[2]
nth-child
例:
#test > li:nth-child(4) //表示ID为test下的第4个子元素,并且限制为li元素,如果不是则报错
解:# 表ID
>表子元素(父子关系)
nth n表第几个
type 同类型
例子2:
<div class="subdiv">
<ul id="recordlist">
<p>Heading</p>
<li>Cat</li>
<li>Dog</li>
<li>Car</li>
<li>Goat</li>
</ul>
</div>
以上内容为大家介绍了自动化测试中的Selenium CSS页面元素定位,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/