@@ -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,7 +21,7 @@ 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"
@@ -77,7 +56,7 @@ For instance, consider this page source::
77
56
78
57
The form element can be located like this::
79
58
80
- login_form = driver.find_element_by_id( 'loginForm')
59
+ login_form = driver.find_element(By.ID, 'loginForm')
81
60
82
61
83
62
Locating by Name
@@ -103,12 +82,12 @@ For instance, consider this page source::
103
82
104
83
The username & password elements can be located like this::
105
84
106
- username = driver.find_element_by_name( 'username')
107
- password = driver.find_element_by_name( 'password')
85
+ username = driver.find_element(By.NAME, 'username')
86
+ password = driver.find_element(By.NAME, 'password')
108
87
109
88
This will give the "Login" button as it occurs before the "Clear" button::
110
89
111
- continue = driver.find_element_by_name( 'continue')
90
+ continue = driver.find_element(By.NAME, 'continue')
112
91
113
92
114
93
Locating by XPath
@@ -148,9 +127,9 @@ For instance, consider this page source::
148
127
149
128
The form elements can be located like this::
150
129
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']")
130
+ login_form = driver.find_element(By.XPATH "/html/body/form[1]")
131
+ login_form = driver.find_element(By.XPATH, "//form[1]")
132
+ login_form = driver.find_element(By.XPATH, "//form[@id='loginForm']")
154
133
155
134
156
135
1. Absolute path (would break if the HTML was changed only slightly)
@@ -161,9 +140,9 @@ The form elements can be located like this::
161
140
162
141
The username element can be located like this::
163
142
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']")
143
+ username = driver.find_element(By.XPATH, "//form[input/@name='username']")
144
+ username = driver.find_element(By.XPATH, "//form[@id='loginForm']/input[1]")
145
+ username = driver.find_element(By.XPATH, "//input[@name='username']")
167
146
168
147
1. First form element with an input child element with `name ` set to `username `
169
148
@@ -174,8 +153,8 @@ The username element can be located like this::
174
153
175
154
The "Clear" button element can be located like this::
176
155
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]")
156
+ clear_button = driver.find_element(By.XPATH, "//input[@name='continue'][@type='button']")
157
+ clear_button = driver.find_element(By.XPATH, "//form[@id='loginForm']/input[4]")
179
158
180
159
181
160
1. Input with attribute `name ` set to `continue ` and attribute `type ` set to
@@ -224,8 +203,8 @@ For instance, consider this page source::
224
203
225
204
The continue.html link can be located like this::
226
205
227
- continue_link = driver.find_element_by_link_text( 'Continue')
228
- continue_link = driver.find_element_by_partial_link_text( 'Conti')
206
+ continue_link = driver.find_element(By.LINK_TEXT, 'Continue')
207
+ continue_link = driver.find_element(By.PARTIAL_LINK_TEXT, 'Conti')
229
208
230
209
231
210
Locating Elements by Tag Name
@@ -246,7 +225,7 @@ For instance, consider this page source::
246
225
247
226
The heading (h1) element can be located like this::
248
227
249
- heading1 = driver.find_element_by_tag_name( 'h1')
228
+ heading1 = driver.find_element(By.TAG_NAME, 'h1')
250
229
251
230
252
231
Locating Elements by Class Name
@@ -267,7 +246,7 @@ For instance, consider this page source::
267
246
268
247
The "p" element can be located like this::
269
248
270
- content = driver.find_element_by_class_name( 'content')
249
+ content = driver.find_element(By.CLASS_NAME, 'content')
271
250
272
251
Locating Elements by CSS Selectors
273
252
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -288,7 +267,7 @@ For instance, consider this page source::
288
267
289
268
The "p" element can be located like this::
290
269
291
- content = driver.find_element_by_css_selector( 'p.content')
270
+ content = driver.find_element(By.CSS_SELECTOR, 'p.content')
292
271
293
272
`Sauce Labs has good documentation
294
273
<https://saucelabs.com/resources/articles/selenium-tips-css-selectors> `_ on CSS
0 commit comments