@@ -4,34 +4,13 @@ Locating Elements
4
4
-----------------
5
5
6
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
7
+ appropriate one for your case. Selenium provides the following method to
8
8
locate elements in a page:
9
9
10
- - `find_element_by_id `
11
- - `find_element_by_name `
12
- - `find_element_by_xpath `
13
- - `find_element_by_link_text `
14
- - `find_element_by_partial_link_text `
15
- - `find_element_by_tag_name `
16
- - `find_element_by_class_name `
17
- - `find_element_by_css_selector `
18
-
10
+ - `find_element `
19
11
20
12
**To find multiple elements (these methods will return a list): **
21
13
22
- - `find_elements_by_name `
23
- - `find_elements_by_xpath `
24
- - `find_elements_by_link_text `
25
- - `find_elements_by_partial_link_text `
26
- - `find_elements_by_tag_name `
27
- - `find_elements_by_class_name `
28
- - `find_elements_by_css_selector `
29
-
30
-
31
- Apart from the public methods given above, there are two private methods which
32
- might be useful for locating page elements:
33
-
34
- - `find_element `
35
14
- `find_elements `
36
15
37
16
@@ -42,18 +21,32 @@ Example usage::
42
21
driver.find_element(By.XPATH, '//button[text()="Some text"]')
43
22
driver.find_elements(By.XPATH, '//button')
44
23
45
-
24
+ The attributes available for the ` By ` class are used to locate elements on a page.
46
25
These are the attributes available for `By ` class::
47
26
48
27
ID = "id"
28
+ NAME = "name"
49
29
XPATH = "xpath"
50
30
LINK_TEXT = "link text"
51
31
PARTIAL_LINK_TEXT = "partial link text"
52
- NAME = "name"
53
32
TAG_NAME = "tag name"
54
33
CLASS_NAME = "class name"
55
34
CSS_SELECTOR = "css selector"
56
35
36
+ The 'By' class is used to specify which attribute is used to locate elements on a page.
37
+ These are the various ways the attributes are used to locate elements on a page::
38
+
39
+ find_element(By.ID, "id")
40
+ find_element(By.NAME, "name")
41
+ find_element(By.XPATH, "xpath")
42
+ find_element(By.LINK_TEXT, "link text")
43
+ find_element(By.PARTIAL_LINK_TEXT, "partial link text")
44
+ find_element(By.TAG_NAME, "tag name")
45
+ find_element(By.CLASS_NAME, "class name")
46
+ find_element(By.CSS_SELECTOR, "css selector")
47
+
48
+ If you want to locate several elements with the same attribute replace find_element with find_elements.
49
+
57
50
58
51
Locating by Id
59
52
~~~~~~~~~~~~~~
@@ -77,7 +70,7 @@ For instance, consider this page source::
77
70
78
71
The form element can be located like this::
79
72
80
- login_form = driver.find_element_by_id( 'loginForm')
73
+ login_form = driver.find_element(By.ID, 'loginForm')
81
74
82
75
83
76
Locating by Name
@@ -103,12 +96,12 @@ For instance, consider this page source::
103
96
104
97
The username & password elements can be located like this::
105
98
106
- username = driver.find_element_by_name( 'username')
107
- password = driver.find_element_by_name( 'password')
99
+ username = driver.find_element(By.NAME, 'username')
100
+ password = driver.find_element(By.NAME, 'password')
108
101
109
102
This will give the "Login" button as it occurs before the "Clear" button::
110
103
111
- continue = driver.find_element_by_name( 'continue')
104
+ continue = driver.find_element(By.NAME, 'continue')
112
105
113
106
114
107
Locating by XPath
@@ -148,9 +141,9 @@ For instance, consider this page source::
148
141
149
142
The form elements can be located like this::
150
143
151
- login_form = driver.find_element_by_xpath( "/html/body/form[1]")
152
- login_form = driver.find_element_by_xpath( "//form[1]")
153
- login_form = driver.find_element_by_xpath( "//form[@id='loginForm']")
144
+ login_form = driver.find_element(By.XPATH, "/html/body/form[1]")
145
+ login_form = driver.find_element(By.XPATH, "//form[1]")
146
+ login_form = driver.find_element(By.XPATH, "//form[@id='loginForm']")
154
147
155
148
156
149
1. Absolute path (would break if the HTML was changed only slightly)
@@ -161,9 +154,9 @@ The form elements can be located like this::
161
154
162
155
The username element can be located like this::
163
156
164
- username = driver.find_element_by_xpath( "//form[input/@name='username']")
165
- username = driver.find_element_by_xpath( "//form[@id='loginForm']/input[1]")
166
- username = driver.find_element_by_xpath( "//input[@name='username']")
157
+ username = driver.find_element(By.XPATH, "//form[input/@name='username']")
158
+ username = driver.find_element(By.XPATH, "//form[@id='loginForm']/input[1]")
159
+ username = driver.find_element(By.XPATH, "//input[@name='username']")
167
160
168
161
1. First form element with an input child element with `name ` set to `username `
169
162
@@ -174,8 +167,8 @@ The username element can be located like this::
174
167
175
168
The "Clear" button element can be located like this::
176
169
177
- clear_button = driver.find_element_by_xpath( "//input[@name='continue'][@type='button']")
178
- clear_button = driver.find_element_by_xpath( "//form[@id='loginForm']/input[4]")
170
+ clear_button = driver.find_element(By.XPATH, "//input[@name='continue'][@type='button']")
171
+ clear_button = driver.find_element(By.XPATH, "//form[@id='loginForm']/input[4]")
179
172
180
173
181
174
1. Input with attribute `name ` set to `continue ` and attribute `type ` set to
@@ -224,8 +217,8 @@ For instance, consider this page source::
224
217
225
218
The continue.html link can be located like this::
226
219
227
- continue_link = driver.find_element_by_link_text( 'Continue')
228
- continue_link = driver.find_element_by_partial_link_text( 'Conti')
220
+ continue_link = driver.find_element(By.LINK_TEXT, 'Continue')
221
+ continue_link = driver.find_element(By.PARTIAL_LINK_TEXT, 'Conti')
229
222
230
223
231
224
Locating Elements by Tag Name
@@ -246,7 +239,7 @@ For instance, consider this page source::
246
239
247
240
The heading (h1) element can be located like this::
248
241
249
- heading1 = driver.find_element_by_tag_name( 'h1')
242
+ heading1 = driver.find_element(By.TAG_NAME, 'h1')
250
243
251
244
252
245
Locating Elements by Class Name
@@ -267,7 +260,7 @@ For instance, consider this page source::
267
260
268
261
The "p" element can be located like this::
269
262
270
- content = driver.find_element_by_class_name( 'content')
263
+ content = driver.find_element(By.CLASS_NAME, 'content')
271
264
272
265
Locating Elements by CSS Selectors
273
266
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -288,7 +281,7 @@ For instance, consider this page source::
288
281
289
282
The "p" element can be located like this::
290
283
291
- content = driver.find_element_by_css_selector( 'p.content')
284
+ content = driver.find_element(By.CSS_SELECTOR, 'p.content')
292
285
293
286
`Sauce Labs has good documentation
294
287
<https://saucelabs.com/resources/articles/selenium-tips-css-selectors> `_ on CSS
0 commit comments