Skip to content

Commit 4724be0

Browse files
committed
Enhance docs of different locator strategies.
Also added more XPath examples. This ought to cover enhancements proposed by #940.
1 parent 030e845 commit 4724be0

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

src/SeleniumLibrary/__init__.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,17 @@ class SeleniumLibrary(DynamicCore):
100100
| `Click Element` | name:foo | # Find element with name ``foo``. |
101101
| `Click Element` | default:name:foo | # Use default strategy with value ``name:foo``. |
102102
| `Click Element` | //foo | # Find element using XPath ``//foo``. |
103-
| `Click Element` | default://foo | # Use default strategy with value ``//foo``. |
103+
| `Click Element` | default: //foo | # Use default strategy with value ``//foo``. |
104104
105105
=== Explicit locator strategy ===
106106
107107
The explicit locator strategy is specified with a prefix using either
108108
syntax ``strategy:value`` or ``strategy=value``. The former syntax
109109
is preferred, because the latter is identical to Robot Framework's
110110
[http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#named-argument-syntax|
111-
named argument syntax] and that can cause problems. Notice that the
112-
``strategy:value`` syntax is olny supported by SeleniumLibrary 3.0 and
113-
newer, though.
111+
named argument syntax] and that can cause problems. Spaces around
112+
the separator are ignored, so ``id:foo``, ``id: foo`` and ``id : foo``
113+
are all equivalent.
114114
115115
Locator strategies that are supported by default are listed in the table
116116
below. In addition to them, it is possible to register `custom locators`.
@@ -135,20 +135,33 @@ class SeleniumLibrary(DynamicCore):
135135
prefix is only necessary if the locator value itself accidentally
136136
matches some of the explicit strategies.
137137
138-
Spaces around the separator are ignored, so ``id:foo``, ``id: foo``
139-
and ``id : foo`` are all equivalent.
138+
Different locator strategies have different pros and cons. Using ids,
139+
either explicitly like ``id:foo`` or by using the `default locator
140+
strategy` simply like ``foo``, is recommended when possible, because
141+
the syntax is simple and locating elements by an id is fast for browsers.
142+
If an element does not have an id or the id is not stable, other
143+
solutions need to be used. If an element has a unique tag name or class,
144+
using ``tag``, ``class`` or ``css`` strategy like ``tag:h1``,
145+
``class:example`` or ``css:h1.example`` is often an easy solution. In
146+
more complex cases using XPath expressions is typically the best
147+
approach. They are very powerful but a downside is that they can also
148+
get complex.
140149
141150
Examples:
142151
143-
| `Click Element` | id:container |
144-
| `Click Element` | css:div#container h1 |
145-
| `Click Element` | xpath: //div[@id="container"]//h1 |
152+
| `Click Element` | id:foo | # Element with id 'foo'. |
153+
| `Click Element` | css:div#foo h1 | # h1 element under div with id 'foo'. |
154+
| `Click Element` | xpath: //div[@id="foo"]//h1 | # Same as the above using XPath, not CSS. |
155+
| `Click Element` | xpath: //*[contains(text(), "example")] | # Element containing text 'example'. |
146156
147-
Notice that using the ``sizzle`` strategy or its alias ``jquery``
148-
requires that the system under test contains the jQuery library.
157+
*NOTE:*
149158
150-
Notice also that prior to SeleniumLibrary 3.0, table related keywords
151-
only supported ``xpath``, ``css`` and ``sizzle/jquery`` strategies.
159+
- The ``strategy:value`` syntax is only supported by SeleniumLibrary 3.0
160+
and newer.
161+
- Using the ``sizzle`` strategy or its alias ``jquery`` requires that
162+
the system under test contains the jQuery library.
163+
- Prior to SeleniumLibrary 3.0, table related keywords only supported
164+
``xpath``, ``css`` and ``sizzle/jquery`` strategies.
152165
153166
=== Implicit XPath strategy ===
154167
@@ -158,8 +171,8 @@ class SeleniumLibrary(DynamicCore):
158171
159172
Examples:
160173
161-
| `Click Element` | //div[@id="container"] |
162-
| `Click Element` | (//div)[2] |
174+
| `Click Element` | //div[@id="foo"]//h1 |
175+
| `Click Element` | (//div)[2] |
163176
164177
The support for the ``(//`` prefix is new in SeleniumLibrary 3.0.
165178

0 commit comments

Comments
 (0)