SeleniumLibrary 元素定位


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

strategy:value

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

第二种:

strategy=value

这种语法是 Robot Framework 通常所使用的命令参数的语法。

SeleniumLibrary 支持的元素方法:

= 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 都是等价的。

例如:

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

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

例如:

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

除了可以直接操作元素外,也可以通过 Get WebElement 关键字获取元素对象。

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

如果想更详细的了解元素定位的用法,可以阅读 SeleniumLibrary 库中的 __init__.py 文件