Posted on September 28th, 2017
SeleniumLibrary element positioning
SeleniumLibrary provides two explicit locator strategies for specifying prefixes. The first:
strategy:value
This syntax only supports SeleniumLibrary version 3 and above, and is a new positioning method.
The second:
strategy=value
This syntax is the syntax for command parameters that Robot Framework typically uses.
Element methods supported by 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 |
judgment | 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 |
Spaces separating symbols are ignored, so , id : foo
, id: foo
and id:foo
are equivalent.
E.g:
demo | – |
---|---|
Click Element | id:container |
Click Element | css:div#container h1 |
Click Element | xpath: //div[@id=“container”]//h1 |
If the locator begins with "//" or "(//" it is treated as an Xpath locator. In other words, using //div
and xpath://div
are equivalent.
E.g:
demo | – |
---|---|
Click Element | //div[@id=“container”] |
Click Element | (//div)[2] |
除了可以直接操作元素外,也可以通过 Get WebElement 关键字获取元素对象。
demo | – | – |
---|---|---|
${elem} = | Get WebElement | id=example |
Click Element | ${elem} |
如果想更详细的了解元素定位的用法,可以阅读 SeleniumLibrary 库中的 __init__.py 文件