Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/common/driver_element_finding_tests.py
1865 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
import pytest
19
20
from selenium.common.exceptions import InvalidSelectorException, NoSuchElementException
21
from selenium.webdriver.common.by import By
22
23
# By.id positive
24
25
26
def test_should_be_able_to_find_asingle_element_by_id(driver, pages):
27
pages.load("xhtmlTest.html")
28
element = driver.find_element(By.ID, "linkId")
29
assert element.get_attribute("id") == "linkId"
30
31
32
def test_should_be_able_to_find_asingle_element_by_numeric_id(driver, pages):
33
pages.load("nestedElements.html")
34
element = driver.find_element(By.ID, "2")
35
assert element.get_attribute("id") == "2"
36
37
38
def test_should_be_able_to_find_an_element_with_css_escape(driver, pages):
39
pages.load("idElements.html")
40
element = driver.find_element(By.ID, "with.dots")
41
assert element.get_attribute("id") == "with.dots"
42
43
44
def test_should_be_able_to_find_multiple_elements_by_id(driver, pages):
45
pages.load("nestedElements.html")
46
elements = driver.find_elements(By.ID, "test_id")
47
assert len(elements) == 2
48
49
50
def test_should_be_able_to_find_multiple_elements_by_numeric_id(driver, pages):
51
pages.load("nestedElements.html")
52
elements = driver.find_elements(By.ID, "2")
53
assert len(elements) == 8
54
55
56
# By.id negative
57
58
59
def test_should_not_be_able_to_locate_by_id_asingle_element_that_does_not_exist(driver, pages):
60
pages.load("formPage.html")
61
with pytest.raises(NoSuchElementException):
62
driver.find_element(By.ID, "non_Existent_Button")
63
64
65
def test_should_not_be_able_to_locate_by_id_multiple_elements_that_do_not_exist(driver, pages):
66
pages.load("formPage.html")
67
elements = driver.find_elements(By.ID, "non_Existent_Button")
68
assert len(elements) == 0
69
70
71
def test_finding_asingle_element_by_empty_id_should_throw(driver, pages):
72
pages.load("formPage.html")
73
with pytest.raises(NoSuchElementException):
74
driver.find_element(By.ID, "")
75
76
77
def test_finding_multiple_elements_by_empty_id_should_return_empty_list(driver, pages):
78
pages.load("formPage.html")
79
elements = driver.find_elements(By.ID, "")
80
assert len(elements) == 0
81
82
83
def test_finding_asingle_element_by_id_with_space_should_throw(driver, pages):
84
pages.load("formPage.html")
85
with pytest.raises(NoSuchElementException):
86
driver.find_element(By.ID, "nonexistent button")
87
88
89
def test_finding_multiple_elements_by_id_with_space_should_return_empty_list(driver, pages):
90
pages.load("formPage.html")
91
elements = driver.find_elements(By.ID, "nonexistent button")
92
assert len(elements) == 0
93
94
95
def test_no_such_element_error(driver, pages):
96
pages.load("formPage.html")
97
msg = r"\/errors#nosuchelementexception"
98
with pytest.raises(NoSuchElementException, match=msg):
99
driver.find_element(By.ID, "non_Existent_Button")
100
101
102
# By.name positive
103
104
105
def test_should_be_able_to_find_asingle_element_by_name(driver, pages):
106
pages.load("formPage.html")
107
element = driver.find_element(By.NAME, "checky")
108
assert element.get_attribute("value") == "furrfu"
109
110
111
def test_should_be_able_to_find_multiple_elements_by_name(driver, pages):
112
pages.load("nestedElements.html")
113
elements = driver.find_elements(By.NAME, "checky")
114
assert len(elements) > 1
115
116
117
def test_should_be_able_to_find_an_element_that_does_not_support_the_name_property(driver, pages):
118
pages.load("nestedElements.html")
119
element = driver.find_element(By.NAME, "div1")
120
assert element.get_attribute("name") == "div1"
121
122
123
# By.name negative
124
125
126
def test_should_not_be_able_to_locate_by_name_asingle_element_that_does_not_exist(driver, pages):
127
pages.load("formPage.html")
128
with pytest.raises(NoSuchElementException):
129
driver.find_element(By.NAME, "non_Existent_Button")
130
131
132
def test_should_not_be_able_to_locate_by_name_multiple_elements_that_do_not_exist(driver, pages):
133
pages.load("formPage.html")
134
elements = driver.find_elements(By.NAME, "non_Existent_Button")
135
assert len(elements) == 0
136
137
138
def test_finding_asingle_element_by_empty_name_should_throw(driver, pages):
139
pages.load("formPage.html")
140
with pytest.raises(NoSuchElementException):
141
driver.find_element(By.NAME, "")
142
143
144
def test_finding_multiple_elements_by_empty_name_should_return_empty_list(driver, pages):
145
pages.load("formPage.html")
146
elements = driver.find_elements(By.NAME, "")
147
assert len(elements) == 0
148
149
150
def test_finding_asingle_element_by_name_with_space_should_throw(driver, pages):
151
pages.load("formPage.html")
152
with pytest.raises(NoSuchElementException):
153
driver.find_element(By.NAME, "nonexistent button")
154
155
156
def test_finding_multiple_elements_by_name_with_space_should_return_empty_list(driver, pages):
157
pages.load("formPage.html")
158
elements = driver.find_elements(By.NAME, "nonexistent button")
159
assert len(elements) == 0
160
161
162
# By.tag_Name positive
163
164
165
def test_should_be_able_to_find_asingle_element_by_tag_name(driver, pages):
166
pages.load("formPage.html")
167
element = driver.find_element(By.TAG_NAME, "input")
168
assert element.tag_name.lower() == "input"
169
170
171
def test_should_be_able_to_find_multiple_elements_by_tag_name(driver, pages):
172
pages.load("formPage.html")
173
elements = driver.find_elements(By.TAG_NAME, "input")
174
assert len(elements) > 1
175
176
177
# By.tag_Name negative
178
179
180
def test_should_not_be_able_to_locate_by_tag_name_asingle_element_that_does_not_exist(driver, pages):
181
pages.load("formPage.html")
182
with pytest.raises(NoSuchElementException):
183
driver.find_element(By.TAG_NAME, "non_Existent_Button")
184
185
186
def test_should_not_be_able_to_locate_by_tag_name_multiple_elements_that_do_not_exist(driver, pages):
187
pages.load("formPage.html")
188
elements = driver.find_elements(By.TAG_NAME, "non_Existent_Button")
189
assert len(elements) == 0
190
191
192
@pytest.mark.xfail_firefox(reason="https://github.com/mozilla/geckodriver/issues/2007")
193
@pytest.mark.xfail_remote(reason="https://github.com/mozilla/geckodriver/issues/2007")
194
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises NoSuchElementException")
195
@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
196
@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
197
def test_finding_asingle_element_by_empty_tag_name_should_throw(driver, pages):
198
pages.load("formPage.html")
199
with pytest.raises(InvalidSelectorException):
200
driver.find_element(By.TAG_NAME, "")
201
202
203
@pytest.mark.xfail_firefox(reason="https://github.com/mozilla/geckodriver/issues/2007")
204
@pytest.mark.xfail_remote(reason="https://github.com/mozilla/geckodriver/issues/2007")
205
@pytest.mark.xfail_safari(reason="unlike chrome, safari returns an empty list")
206
@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
207
@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
208
def test_finding_multiple_elements_by_empty_tag_name_should_throw(driver, pages):
209
pages.load("formPage.html")
210
with pytest.raises(InvalidSelectorException):
211
driver.find_elements(By.TAG_NAME, "")
212
213
214
def test_finding_asingle_element_by_tag_name_with_space_should_throw(driver, pages):
215
pages.load("formPage.html")
216
with pytest.raises(NoSuchElementException):
217
driver.find_element(By.TAG_NAME, "nonexistent button")
218
219
220
def test_finding_multiple_elements_by_tag_name_with_space_should_return_empty_list(driver, pages):
221
pages.load("formPage.html")
222
elements = driver.find_elements(By.TAG_NAME, "nonexistent button")
223
assert len(elements) == 0
224
225
226
# By.class_Name positive
227
228
229
def test_should_be_able_to_find_asingle_element_by_class(driver, pages):
230
pages.load("xhtmlTest.html")
231
element = driver.find_element(By.CLASS_NAME, "extraDiv")
232
assert "Another div starts here." in element.text
233
234
235
def test_should_be_able_to_find_multiple_elements_by_class_name(driver, pages):
236
pages.load("xhtmlTest.html")
237
elements = driver.find_elements(By.CLASS_NAME, "nameC")
238
assert len(elements) > 1
239
240
241
def test_should_find_element_by_class_when_it_is_the_first_name_among_many(driver, pages):
242
pages.load("xhtmlTest.html")
243
element = driver.find_element(By.CLASS_NAME, "nameA")
244
assert element.text == "An H2 title"
245
246
247
def test_should_find_element_by_class_when_it_is_the_last_name_among_many(driver, pages):
248
pages.load("xhtmlTest.html")
249
element = driver.find_element(By.CLASS_NAME, "nameC")
250
assert element.text == "An H2 title"
251
252
253
def test_should_find_element_by_class_when_it_is_in_the_middle_among_many(driver, pages):
254
pages.load("xhtmlTest.html")
255
element = driver.find_element(By.CLASS_NAME, "nameBnoise")
256
assert element.text == "An H2 title"
257
258
259
def test_should_find_element_by_class_when_its_name_is_surrounded_by_whitespace(driver, pages):
260
pages.load("xhtmlTest.html")
261
element = driver.find_element(By.CLASS_NAME, "spaceAround")
262
assert element.text == "Spaced out"
263
264
265
def test_should_find_elements_by_class_when_its_name_is_surrounded_by_whitespace(driver, pages):
266
pages.load("xhtmlTest.html")
267
elements = driver.find_elements(By.CLASS_NAME, "spaceAround")
268
assert len(elements) == 1
269
assert elements[0].text == "Spaced out"
270
271
272
# By.class_Name negative
273
274
275
def test_should_not_find_element_by_class_when_the_name_queried_is_shorter_than_candidate_name(driver, pages):
276
pages.load("xhtmlTest.html")
277
with pytest.raises(NoSuchElementException):
278
driver.find_element(By.CLASS_NAME, "name_B")
279
280
281
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
282
@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
283
@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
284
def test_finding_asingle_element_by_empty_class_name_should_throw(driver, pages):
285
pages.load("xhtmlTest.html")
286
msg = r"\/errors#invalidselectorexception"
287
with pytest.raises(InvalidSelectorException, match=msg):
288
driver.find_element(By.CLASS_NAME, "")
289
290
291
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
292
@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
293
@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
294
def test_finding_multiple_elements_by_empty_class_name_should_throw(driver, pages):
295
pages.load("xhtmlTest.html")
296
with pytest.raises(InvalidSelectorException):
297
driver.find_elements(By.CLASS_NAME, "")
298
299
300
def test_finding_asingle_element_by_compound_class_name_should_throw(driver, pages):
301
pages.load("xhtmlTest.html")
302
with pytest.raises(NoSuchElementException):
303
driver.find_element(By.CLASS_NAME, "a b")
304
305
306
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
307
@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
308
@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
309
def test_finding_asingle_element_by_invalid_class_name_should_throw(driver, pages):
310
pages.load("xhtmlTest.html")
311
with pytest.raises(InvalidSelectorException):
312
driver.find_element(By.CLASS_NAME, "!@#$%^&*")
313
314
315
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
316
@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
317
@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
318
def test_finding_multiple_elements_by_invalid_class_name_should_throw(driver, pages):
319
pages.load("xhtmlTest.html")
320
with pytest.raises(InvalidSelectorException):
321
driver.find_elements(By.CLASS_NAME, "!@#$%^&*")
322
323
324
# By.xpath positive
325
326
327
def test_should_be_able_to_find_asingle_element_by_xpath(driver, pages):
328
pages.load("xhtmlTest.html")
329
element = driver.find_element(By.XPATH, "//h1")
330
assert element.text == "XHTML Might Be The Future"
331
332
333
def test_should_be_able_to_find_multiple_elements_by_xpath(driver, pages):
334
pages.load("xhtmlTest.html")
335
elements = driver.find_elements(By.XPATH, "//div")
336
assert len(elements) == 13
337
338
339
def test_should_be_able_to_find_many_elements_repeatedly_by_xpath(driver, pages):
340
pages.load("xhtmlTest.html")
341
xpath = "//node()[contains(@id,'id')]"
342
assert len(driver.find_elements(By.XPATH, xpath)) == 3
343
344
xpath = "//node()[contains(@id,'nope')]"
345
assert len(driver.find_elements(By.XPATH, xpath)) == 0
346
347
348
def test_should_be_able_to_identify_elements_by_class(driver, pages):
349
pages.load("xhtmlTest.html")
350
header = driver.find_element(By.XPATH, "//h1[@class='header']")
351
assert header.text == "XHTML Might Be The Future"
352
353
354
def test_should_be_able_to_find_an_element_by_xpath_with_multiple_attributes(driver, pages):
355
pages.load("formPage.html")
356
element = driver.find_element(By.XPATH, "//form[@name='optional']/input[@type='submit' and @value='Click!']")
357
assert element.tag_name.lower() == "input"
358
assert element.get_attribute("value") == "Click!"
359
360
361
def test_finding_alink_by_xpath_should_locate_an_element_with_the_given_text(driver, pages):
362
pages.load("xhtmlTest.html")
363
element = driver.find_element(By.XPATH, "//a[text()='click me']")
364
assert element.text == "click me"
365
366
367
def test_finding_alink_by_xpath_using_contains_keyword_should_work(driver, pages):
368
pages.load("nestedElements.html")
369
element = driver.find_element(By.XPATH, "//a[contains(.,'hello world')]")
370
assert "hello world" in element.text
371
372
373
# @pytest.mark.xfail_chrome(raises=InvalidSelectorException)
374
# @pytest.mark.xfail_edge(raises=InvalidSelectorException)
375
# @pytest.mark.xfail_firefox(raises=InvalidSelectorException)
376
# @pytest.mark.xfail_remote(raises=InvalidSelectorException)
377
# @pytest.mark.xfail_safari(raises=NoSuchElementException)
378
# @pytest.mark.xfail_webkitgtk(raises=InvalidSelectorException)
379
# def test_Should_Be_Able_To_Find_Element_By_XPath_With_Namespace(driver, pages):
380
# pages.load("svgPage.html")
381
# element = driver.find_element(By.XPATH, "//svg:svg//svg:text")
382
# assert element.text == "Test Chart"
383
384
385
def test_should_be_able_to_find_element_by_xpath_in_xml_document(driver, pages):
386
pages.load("simple.xml")
387
element = driver.find_element(By.XPATH, "//foo")
388
assert "baz" in element.text
389
390
391
# By.xpath negative
392
393
394
def test_should_throw_an_exception_when_there_is_no_link_to_click(driver, pages):
395
pages.load("xhtmlTest.html")
396
with pytest.raises(NoSuchElementException):
397
driver.find_element(By.XPATH, "//a[@id='Not here']")
398
399
400
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
401
@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
402
@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
403
def test_should_throw_invalid_selector_exception_when_xpath_is_syntactically_invalid_in_driver_find_element(
404
driver, pages
405
):
406
pages.load("formPage.html")
407
with pytest.raises(InvalidSelectorException):
408
driver.find_element(By.XPATH, "this][isnot][valid")
409
410
411
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
412
@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
413
@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
414
def test_should_throw_invalid_selector_exception_when_xpath_is_syntactically_invalid_in_driver_find_elements(
415
driver, pages
416
):
417
pages.load("formPage.html")
418
with pytest.raises(InvalidSelectorException):
419
driver.find_elements(By.XPATH, "this][isnot][valid")
420
421
422
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
423
@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
424
@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
425
def test_should_throw_invalid_selector_exception_when_xpath_is_syntactically_invalid_in_element_find_element(
426
driver, pages
427
):
428
pages.load("formPage.html")
429
body = driver.find_element(By.TAG_NAME, "body")
430
with pytest.raises(InvalidSelectorException):
431
body.find_element(By.XPATH, "this][isnot][valid")
432
433
434
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
435
@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
436
@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
437
def test_should_throw_invalid_selector_exception_when_xpath_is_syntactically_invalid_in_element_find_elements(
438
driver, pages
439
):
440
pages.load("formPage.html")
441
body = driver.find_element(By.TAG_NAME, "body")
442
with pytest.raises(InvalidSelectorException):
443
body.find_elements(By.XPATH, "this][isnot][valid")
444
445
446
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
447
@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
448
@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
449
def test_should_throw_invalid_selector_exception_when_xpath_returns_wrong_type_in_driver_find_element(driver, pages):
450
pages.load("formPage.html")
451
with pytest.raises(InvalidSelectorException):
452
driver.find_element(By.XPATH, "count(//input)")
453
454
455
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
456
@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
457
@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
458
def test_should_throw_invalid_selector_exception_when_xpath_returns_wrong_type_in_driver_find_elements(driver, pages):
459
pages.load("formPage.html")
460
with pytest.raises(InvalidSelectorException):
461
driver.find_elements(By.XPATH, "count(//input)")
462
463
464
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
465
@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
466
@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
467
def test_should_throw_invalid_selector_exception_when_xpath_returns_wrong_type_in_element_find_element(driver, pages):
468
pages.load("formPage.html")
469
body = driver.find_element(By.TAG_NAME, "body")
470
with pytest.raises(InvalidSelectorException):
471
body.find_element(By.XPATH, "count(//input)")
472
473
474
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
475
@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
476
@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
477
def test_should_throw_invalid_selector_exception_when_xpath_returns_wrong_type_in_element_find_elements(driver, pages):
478
pages.load("formPage.html")
479
body = driver.find_element(By.TAG_NAME, "body")
480
with pytest.raises(InvalidSelectorException):
481
body.find_elements(By.XPATH, "count(//input)")
482
483
484
# By.css_Selector positive
485
486
487
def test_should_be_able_to_find_asingle_element_by_css_selector(driver, pages):
488
pages.load("xhtmlTest.html")
489
element = driver.find_element(By.CSS_SELECTOR, "div.content")
490
assert element.tag_name.lower() == "div"
491
assert element.get_attribute("class") == "content"
492
493
494
def test_should_be_able_to_find_multiple_elements_by_css_selector(driver, pages):
495
pages.load("xhtmlTest.html")
496
elements = driver.find_elements(By.CSS_SELECTOR, "p")
497
assert len(elements) > 1
498
499
500
def test_should_be_able_to_find_asingle_element_by_compound_css_selector(driver, pages):
501
pages.load("xhtmlTest.html")
502
element = driver.find_element(By.CSS_SELECTOR, "div.extraDiv, div.content")
503
assert element.tag_name.lower() == "div"
504
assert element.get_attribute("class") == "content"
505
506
507
def test_should_be_able_to_find_multiple_elements_by_compound_css_selector(driver, pages):
508
pages.load("xhtmlTest.html")
509
elements = driver.find_elements(By.CSS_SELECTOR, "div.extraDiv, div.content")
510
assert len(elements) > 1
511
assert elements[0].get_attribute("class") == "content"
512
assert elements[1].get_attribute("class") == "extraDiv"
513
514
515
def test_should_be_able_to_find_an_element_by_boolean_attribute_using_css_selector(driver, pages):
516
pages.load("locators_tests/boolean_attribute_selected.html")
517
element = driver.find_element(By.CSS_SELECTOR, "option[selected='selected']")
518
assert element.get_attribute("value") == "two"
519
520
521
def test_should_be_able_to_find_an_element_by_boolean_attribute_using_short_css_selector(driver, pages):
522
pages.load("locators_tests/boolean_attribute_selected.html")
523
element = driver.find_element(By.CSS_SELECTOR, "option[selected]")
524
assert element.get_attribute("value") == "two"
525
526
527
def test_should_be_able_to_find_an_element_by_boolean_attribute_using_short_css_selector_on_html_4_page(driver, pages):
528
pages.load("locators_tests/boolean_attribute_selected_html4.html")
529
element = driver.find_element(By.CSS_SELECTOR, "option[selected]")
530
assert element.get_attribute("value") == "two"
531
532
533
# By.css_Selector negative
534
535
536
def test_should_not_find_element_by_css_selector_when_there_is_no_such_element(driver, pages):
537
pages.load("xhtmlTest.html")
538
with pytest.raises(NoSuchElementException):
539
driver.find_element(By.CSS_SELECTOR, ".there-is-no-such-class")
540
541
542
def test_should_not_find_elements_by_css_selector_when_there_is_no_such_element(driver, pages):
543
pages.load("xhtmlTest.html")
544
elements = driver.find_elements(By.CSS_SELECTOR, ".there-is-no-such-class")
545
assert len(elements) == 0
546
547
548
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
549
@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
550
@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
551
def test_finding_asingle_element_by_empty_css_selector_should_throw(driver, pages):
552
pages.load("xhtmlTest.html")
553
with pytest.raises(InvalidSelectorException):
554
driver.find_element(By.CSS_SELECTOR, "")
555
556
557
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
558
@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
559
@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
560
def test_finding_multiple_elements_by_empty_css_selector_should_throw(driver, pages):
561
pages.load("xhtmlTest.html")
562
with pytest.raises(InvalidSelectorException):
563
driver.find_elements(By.CSS_SELECTOR, "")
564
565
566
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
567
@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
568
@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
569
def test_finding_asingle_element_by_invalid_css_selector_should_throw(driver, pages):
570
pages.load("xhtmlTest.html")
571
with pytest.raises(InvalidSelectorException):
572
driver.find_element(By.CSS_SELECTOR, "//a/b/c[@id='1']")
573
574
575
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
576
@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
577
@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
578
def test_finding_multiple_elements_by_invalid_css_selector_should_throw(driver, pages):
579
pages.load("xhtmlTest.html")
580
with pytest.raises(InvalidSelectorException):
581
driver.find_elements(By.CSS_SELECTOR, "//a/b/c[@id='1']")
582
583
584
# By.link_Text positive
585
586
587
def test_should_be_able_to_find_alink_by_text(driver, pages):
588
pages.load("xhtmlTest.html")
589
link = driver.find_element(By.LINK_TEXT, "click me")
590
assert link.text == "click me"
591
592
593
def test_should_be_able_to_find_multiple_links_by_text(driver, pages):
594
pages.load("xhtmlTest.html")
595
elements = driver.find_elements(By.LINK_TEXT, "click me")
596
assert len(elements) == 2
597
598
599
def test_should_find_element_by_link_text_containing_equals_sign(driver, pages):
600
pages.load("xhtmlTest.html")
601
element = driver.find_element(By.LINK_TEXT, "Link=equalssign")
602
assert element.get_attribute("id") == "linkWithEqualsSign"
603
604
605
def test_should_find_multiple_elements_by_link_text_containing_equals_sign(driver, pages):
606
pages.load("xhtmlTest.html")
607
elements = driver.find_elements(By.LINK_TEXT, "Link=equalssign")
608
assert 1 == len(elements)
609
assert elements[0].get_attribute("id") == "linkWithEqualsSign"
610
611
612
def test_finds_by_link_text_on_xhtml_page(driver, pages):
613
pages.load("actualXhtmlPage.xhtml")
614
link_Text = "Foo"
615
element = driver.find_element(By.LINK_TEXT, link_Text)
616
assert element.text == link_Text
617
618
619
def test_link_with_formatting_tags(driver, pages):
620
pages.load("simpleTest.html")
621
elem = driver.find_element(By.ID, "links")
622
623
res = elem.find_element(By.PARTIAL_LINK_TEXT, "link with formatting tags")
624
assert res.text == "link with formatting tags"
625
626
627
@pytest.mark.xfail_safari
628
def test_driver_can_get_link_by_link_test_ignoring_trailing_whitespace(driver, pages):
629
pages.load("simpleTest.html")
630
link = driver.find_element(By.LINK_TEXT, "link with trailing space")
631
assert link.get_attribute("id") == "linkWithTrailingSpace"
632
assert link.text == "link with trailing space"
633
634
635
# By.link_Text negative
636
637
638
def test_should_not_be_able_to_locate_by_link_text_asingle_element_that_does_not_exist(driver, pages):
639
pages.load("xhtmlTest.html")
640
with pytest.raises(NoSuchElementException):
641
driver.find_element(By.LINK_TEXT, "Not here either")
642
643
644
def test_should_not_be_able_to_locate_by_link_text_multiple_elements_that_do_not_exist(driver, pages):
645
pages.load("xhtmlTest.html")
646
elements = driver.find_elements(By.LINK_TEXT, "Not here either")
647
assert len(elements) == 0
648
649
650
# By.partial_Link_Text positive
651
652
653
def test_should_be_able_to_find_multiple_elements_by_partial_link_text(driver, pages):
654
pages.load("xhtmlTest.html")
655
elements = driver.find_elements(By.PARTIAL_LINK_TEXT, "ick me")
656
assert len(elements) == 2
657
658
659
def test_should_be_able_to_find_asingle_element_by_partial_link_text(driver, pages):
660
pages.load("xhtmlTest.html")
661
element = driver.find_element(By.PARTIAL_LINK_TEXT, "anon")
662
assert "anon" in element.text
663
664
665
def test_should_find_element_by_partial_link_text_containing_equals_sign(driver, pages):
666
pages.load("xhtmlTest.html")
667
element = driver.find_element(By.PARTIAL_LINK_TEXT, "Link=")
668
assert element.get_attribute("id") == "linkWithEqualsSign"
669
670
671
def test_should_find_multiple_elements_by_partial_link_text_containing_equals_sign(driver, pages):
672
pages.load("xhtmlTest.html")
673
elements = driver.find_elements(By.PARTIAL_LINK_TEXT, "Link=")
674
assert len(elements) == 1
675
assert elements[0].get_attribute("id") == "linkWithEqualsSign"
676
677
678
# Misc tests
679
680
681
def test_driver_should_be_able_to_find_elements_after_loading_more_than_one_page_at_atime(driver, pages):
682
pages.load("formPage.html")
683
pages.load("xhtmlTest.html")
684
link = driver.find_element(By.LINK_TEXT, "click me")
685
assert link.text == "click me"
686
687
688
# You don't want to ask why this is here
689
690
691
def test_when_finding_by_name_should_not_return_by_id(driver, pages):
692
pages.load("formPage.html")
693
694
element = driver.find_element(By.NAME, "id-name1")
695
assert element.get_attribute("value") == "name"
696
697
element = driver.find_element(By.ID, "id-name1")
698
assert element.get_attribute("value") == "id"
699
700
element = driver.find_element(By.NAME, "id-name2")
701
assert element.get_attribute("value") == "name"
702
703
element = driver.find_element(By.ID, "id-name2")
704
assert element.get_attribute("value") == "id"
705
706
707
def test_should_be_able_to_find_ahidden_elements_by_name(driver, pages):
708
pages.load("formPage.html")
709
element = driver.find_element(By.NAME, "hidden")
710
assert element.get_attribute("name") == "hidden"
711
712
713
def test_should_not_be_able_to_find_an_element_on_a_blank_page(driver, pages):
714
driver.get("about:blank")
715
with pytest.raises(NoSuchElementException):
716
driver.find_element(By.TAG_NAME, "a")
717
718
719
# custom finders tests
720
721
722
def test_register_and_get_custom_finder():
723
By.register_custom_finder("custom", "custom strategy")
724
assert By.get_finder("custom") == "custom strategy"
725
726
727
def test_get_nonexistent_finder():
728
assert By.get_finder("nonexistent") is None
729
730
731
def test_clear_custom_finders():
732
By.register_custom_finder("custom", "custom strategy")
733
By.clear_custom_finders()
734
assert By.get_finder("custom") is None
735
736