3
3
Locating Elements
4
4
-----------------
5
5
6
- There are various strategies to locate elements in a page. You can use
7
- the most appropriate one for your case. Selenium provides the following
8
- methods to locate elements in a page:
6
+ There are various strategies to locate elements in a page. You can use the most
7
+ appropriate one for your case. Selenium provides the following methods to
8
+ locate elements in a page:
9
9
10
10
- `find_element_by_id `
11
11
- `find_element_by_name `
@@ -28,8 +28,8 @@ methods to locate elements in a page:
28
28
- `find_elements_by_css_selector `
29
29
30
30
31
- Apart from the public methods given above, there are two private
32
- methods which might be useful for locating page elements:
31
+ Apart from the public methods given above, there are two private methods which
32
+ might be useful for locating page elements:
33
33
34
34
- `find_element `
35
35
- `find_elements `
@@ -58,10 +58,10 @@ These are the attributes available for `By` class::
58
58
Locating by Id
59
59
~~~~~~~~~~~~~~
60
60
61
- Use this when you know the `id ` attribute of an element. With this
62
- strategy, the first element with a matching `id ` attribute will
63
- be returned. If no element has a matching `id ` attribute,
64
- a `` NoSuchElementException `` will be raised.
61
+ Use this when you know the `id ` attribute of an element. With this strategy,
62
+ the first element with a matching `id ` attribute will be returned. If no
63
+ element has a matching `id ` attribute, a `` NoSuchElementException `` will be
64
+ raised.
65
65
66
66
For instance, consider this page source::
67
67
@@ -83,10 +83,10 @@ The form element can be located like this::
83
83
Locating by Name
84
84
~~~~~~~~~~~~~~~~
85
85
86
- Use this when you know the `name ` attribute of an element. With this
87
- strategy, the first element with a matching `name ` attribute will
88
- be returned. If no element has a matching `name ` attribute,
89
- a `` NoSuchElementException `` will be raised.
86
+ Use this when you know the `name ` attribute of an element. With this strategy,
87
+ the first element with a matching `name ` attribute will be returned. If no
88
+ element has a matching `name ` attribute, a `` NoSuchElementException `` will be
89
+ raised.
90
90
91
91
For instance, consider this page source::
92
92
@@ -115,26 +115,24 @@ button::
115
115
Locating by XPath
116
116
~~~~~~~~~~~~~~~~~
117
117
118
- XPath is the language used for locating nodes in an XML document. As
119
- HTML can be an implementation of XML (XHTML), Selenium users can
120
- leverage this powerful language to target elements in their web
121
- applications. XPath supports the simple methods of locating by
122
- id or name attributes and extends them by opening up all sorts
123
- of new possibilities such as locating the third checkbox on the page.
124
-
125
- One of the main reasons for using XPath is when you don't have a
126
- suitable id or name attribute for the element you wish to locate. You
127
- can use XPath to either locate the element in absolute terms (not
128
- advised), or relative to an element that does have an id or name
129
- attribute. XPath locators can also be used to specify elements via
130
- attributes other than id and name.
131
-
132
- Absolute XPaths contain the location of all elements from the root
133
- (html) and as a result are likely to fail with only the slightest
134
- adjustment to the application. By finding a nearby element with an id
135
- or name attribute (ideally a parent element) you can locate your
136
- target element based on the relationship. This is much less likely to
137
- change and can make your tests more robust.
118
+ XPath is the language used for locating nodes in an XML document. As HTML can
119
+ be an implementation of XML (XHTML), Selenium users can leverage this powerful
120
+ language to target elements in their web applications. XPath supports the
121
+ simple methods of locating by id or name attributes and extends them by opening
122
+ up all sorts of new possibilities such as locating the third checkbox on the
123
+ page.
124
+
125
+ One of the main reasons for using XPath is when you don't have a suitable id or
126
+ name attribute for the element you wish to locate. You can use XPath to either
127
+ locate the element in absolute terms (not advised), or relative to an element
128
+ that does have an id or name attribute. XPath locators can also be used to
129
+ specify elements via attributes other than id and name.
130
+
131
+ Absolute XPaths contain the location of all elements from the root (html) and as
132
+ a result are likely to fail with only the slightest adjustment to the
133
+ application. By finding a nearby element with an id or name attribute (ideally
134
+ a parent element) you can locate your target element based on the relationship.
135
+ This is much less likely to change and can make your tests more robust.
138
136
139
137
For instance, consider this page source::
140
138
@@ -170,7 +168,8 @@ The username element can be located like this::
170
168
171
169
1. First form element with an input child element with `name ` set to `username `
172
170
173
- 2. First input child element of the form element with attribute `id ` set to `loginForm `
171
+ 2. First input child element of the form element with attribute `id ` set to
172
+ `loginForm `
174
173
175
174
3. First input element with attribute `name ` set to `username `
176
175
@@ -180,29 +179,27 @@ The "Clear" button element can be located like this::
180
179
clear_button = driver.find_element_by_xpath("//form[@id='loginForm']/input[4]")
181
180
182
181
183
- 1. Input with attribute `name ` set to `continue ` and
184
- attribute ` type ` set to `button `
182
+ 1. Input with attribute `name ` set to `continue ` and attribute ` type ` set to
183
+ `button `
185
184
186
- 2. Fourth input child element of the form element with attribute `id ` set to `loginForm `
185
+ 2. Fourth input child element of the form element with attribute `id ` set to
186
+ `loginForm `
187
187
188
- These examples cover some basics, but in order to learn more, the
189
- following references are recommended:
188
+ These examples cover some basics, but in order to learn more, the following
189
+ references are recommended:
190
190
191
191
* `W3Schools XPath Tutorial <https://www.w3schools.com/xml/xpath_intro.asp >`_
192
192
* `W3C XPath Recommendation <http://www.w3.org/TR/xpath >`_
193
193
* `XPath Tutorial
194
194
<http://www.zvon.org/comp/r/tut-XPath_1.html> `_
195
195
- with interactive examples.
196
196
197
- There are also a couple of very useful Add-ons that can assist in
198
- discovering the XPath of an element:
197
+ Here is a couple of very useful Add-ons that can assist in discovering the XPath
198
+ of an element:
199
199
200
- * `XPath Checker
201
- <https://addons.mozilla.org/en-US/firefox/addon/xpath-checker/> `_ -
202
- suggests XPath and can be used to test XPath results.
203
- * `Firebug <https://addons.mozilla.org/en-US/firefox/addon/firebug/ >`_ -
204
- XPath suggestions are just one of the many powerful features of this
205
- very useful add-on.
200
+ * `xPath Finder
201
+ <https://addons.mozilla.org/en-US/firefox/addon/xpath_finder> `_ -
202
+ Plugin to get the elements xPath.
206
203
* `XPath Helper
207
204
<https://chrome.google.com/webstore/detail/hgimnogjllphhhkhlmebbmlgjoejdpjl> `_ -
208
205
for Google Chrome
@@ -212,9 +209,9 @@ Locating Hyperlinks by Link Text
212
209
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
213
210
214
211
Use this when you know the link text used within an anchor tag. With this
215
- strategy, the first element with the link text matching the provided value
216
- will be returned. If no element has a matching link text
217
- attribute, a ``NoSuchElementException `` will be raised.
212
+ strategy, the first element with the link text matching the provided value will
213
+ be returned. If no element has a matching link text attribute, a
214
+ ``NoSuchElementException `` will be raised.
218
215
219
216
For instance, consider this page source::
220
217
@@ -235,10 +232,9 @@ The continue.html link can be located like this::
235
232
Locating Elements by Tag Name
236
233
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
237
234
238
- Use this when you want to locate an element by tag name. With this
239
- strategy, the first element with the given tag name will be returned.
240
- If no element has a matching tag name, a ``NoSuchElementException ``
241
- will be raised.
235
+ Use this when you want to locate an element by tag name. With this strategy, the
236
+ first element with the given tag name will be returned. If no element has a
237
+ matching tag name, a ``NoSuchElementException `` will be raised.
242
238
243
239
For instance, consider this page source::
244
240
@@ -257,10 +253,10 @@ The heading (h1) element can be located like this::
257
253
Locating Elements by Class Name
258
254
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
259
255
260
- Use this when you want to locate an element by class name.
261
- With this strategy, the first element with the matching class name
262
- attribute will be returned. If no element has a matching class name attribute,
263
- a `` NoSuchElementException `` will be raised.
256
+ Use this when you want to locate an element by class name. With this strategy,
257
+ the first element with the matching class name attribute will be returned. If
258
+ no element has a matching class name attribute, a `` NoSuchElementException ``
259
+ will be raised.
264
260
265
261
For instance, consider this page source::
266
262
@@ -277,10 +273,10 @@ The "p" element can be located like this::
277
273
Locating Elements by CSS Selectors
278
274
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
279
275
280
- Use this when you want to locate an element using CSS selector syntax.
281
- With this strategy, the first element matching the given CSS selector
282
- will be returned. If no element matches the provided CSS selector,
283
- a ``NoSuchElementException `` will be raised.
276
+ Use this when you want to locate an element using CSS selector syntax. With
277
+ this strategy, the first element matching the given CSS selector will be
278
+ returned. If no element matches the provided CSS selector, a
279
+ ``NoSuchElementException `` will be raised.
284
280
285
281
For instance, consider this page source::
286
282
0 commit comments