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 文件