SeleniumLibrary 元素定位


SeleniumLibrary提供了两种指定前缀的显式定位器策略。 第一种:jquery

strategy:value 

这种语法只支持 SeleniumLibrary 3 版本以上,是新的定位写法。git

第二种:github

strategy=value 

这种语法是 Robot Framework 一般所使用的命令参数的语法。express

SeleniumLibrary 支持的元素方法:dom

= Strategy = = Match based on = = Example =
id Element id. id:example
name name attribute. name:example
identifier Either id or name. identifier:example
class Element class. class:example
tag Tag name. tag:div
xpath XPath expression. xpath://div[@id="example"]
css CSS selector. css:div#example
dom DOM expression. dom:document.images[5]
link Exact text a link has. link:The example
partial link Partial link text. partial link:he ex
sizzle Sizzle selector provided by jQuery. sizzle:div.example
jquery Same as the above. jquery:div.example
default Keyword specific default behavior. default:example

分割符号的空格将被忽略, 因此, id : fooid: foo 和 id:foo 都是等价的。ide

例如:post

demo
Click Element id:container
Click Element css:div#container h1
Click Element xpath: //div[@id=“container”]//h1

若是定位器的开头为 “//” 或 “ (// ” 测被当作 Xpath 定位。换句话说,用 //div 和 xpath://div是等价的。spa

例如:code

demo
Click Element //div[@id=“container”]
Click Element (//div)[2]

除了能够直接操做元素外,也能够经过 Get WebElement 关键字获取元素对象。

demo
${elem} = Get WebElement id=example
Click Element ${elem}  

若是想更详细的了解元素定位的用法,能够阅读 SeleniumLibrary 库中的 __init__.py 文件