(七)擴展庫之 SeleniumLibrary 元素定位
阿新 • • 發佈:2018-05-06
除了 ont nbsp element pan lin span BE author
發布時間 2017年9月28日 蟲師
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 : foo
, id: 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 文件
(七)擴展庫之 SeleniumLibrary 元素定位