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
4036 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="unlike chrome, firefox raises NoSuchElementException")
193
@pytest.mark.xfail_remote(reason="unlike chrome, firefox raises NoSuchElementException")
194
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises NoSuchElementException")
195
def test_finding_asingle_element_by_empty_tag_name_should_throw(driver, pages):
196
pages.load("formPage.html")
197
with pytest.raises(InvalidSelectorException):
198
driver.find_element(By.TAG_NAME, "")
199
200
201
@pytest.mark.xfail_firefox(reason="unlike chrome, firefox returns an empty list")
202
@pytest.mark.xfail_remote(reason="unlike chrome, firefox returns an empty list")
203
@pytest.mark.xfail_safari(reason="unlike chrome, safari returns an empty list")
204
def test_finding_multiple_elements_by_empty_tag_name_should_throw(driver, pages):
205
pages.load("formPage.html")
206
with pytest.raises(InvalidSelectorException):
207
driver.find_elements(By.TAG_NAME, "")
208
209
210
def test_finding_asingle_element_by_tag_name_with_space_should_throw(driver, pages):
211
pages.load("formPage.html")
212
with pytest.raises(NoSuchElementException):
213
driver.find_element(By.TAG_NAME, "nonexistent button")
214
215
216
def test_finding_multiple_elements_by_tag_name_with_space_should_return_empty_list(driver, pages):
217
pages.load("formPage.html")
218
elements = driver.find_elements(By.TAG_NAME, "nonexistent button")
219
assert len(elements) == 0
220
221
222
# By.CLASS_NAME positive
223
224
225
def test_should_be_able_to_find_asingle_element_by_class(driver, pages):
226
pages.load("xhtmlTest.html")
227
element = driver.find_element(By.CLASS_NAME, "extraDiv")
228
assert "Another div starts here." in element.text
229
230
231
def test_should_be_able_to_find_multiple_elements_by_class_name(driver, pages):
232
pages.load("xhtmlTest.html")
233
elements = driver.find_elements(By.CLASS_NAME, "nameC")
234
assert len(elements) > 1
235
236
237
def test_should_find_element_by_class_when_it_is_the_first_name_among_many(driver, pages):
238
pages.load("xhtmlTest.html")
239
element = driver.find_element(By.CLASS_NAME, "nameA")
240
assert element.text == "An H2 title"
241
242
243
def test_should_find_element_by_class_when_it_is_the_last_name_among_many(driver, pages):
244
pages.load("xhtmlTest.html")
245
element = driver.find_element(By.CLASS_NAME, "nameC")
246
assert element.text == "An H2 title"
247
248
249
def test_should_find_element_by_class_when_it_is_in_the_middle_among_many(driver, pages):
250
pages.load("xhtmlTest.html")
251
element = driver.find_element(By.CLASS_NAME, "nameBnoise")
252
assert element.text == "An H2 title"
253
254
255
def test_should_find_element_by_class_when_its_name_is_surrounded_by_whitespace(driver, pages):
256
pages.load("xhtmlTest.html")
257
element = driver.find_element(By.CLASS_NAME, "spaceAround")
258
assert element.text == "Spaced out"
259
260
261
def test_should_find_elements_by_class_when_its_name_is_surrounded_by_whitespace(driver, pages):
262
pages.load("xhtmlTest.html")
263
elements = driver.find_elements(By.CLASS_NAME, "spaceAround")
264
assert len(elements) == 1
265
assert elements[0].text == "Spaced out"
266
267
268
# By.CLASS_NAME negative
269
270
271
def test_should_not_find_element_by_class_when_the_name_queried_is_shorter_than_candidate_name(driver, pages):
272
pages.load("xhtmlTest.html")
273
with pytest.raises(NoSuchElementException):
274
driver.find_element(By.CLASS_NAME, "name_B")
275
276
277
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
278
def test_finding_asingle_element_by_empty_class_name_should_throw(driver, pages):
279
pages.load("xhtmlTest.html")
280
msg = r"\/errors#invalidselectorexception"
281
with pytest.raises(InvalidSelectorException, match=msg):
282
driver.find_element(By.CLASS_NAME, "")
283
284
285
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
286
def test_finding_multiple_elements_by_empty_class_name_should_throw(driver, pages):
287
pages.load("xhtmlTest.html")
288
with pytest.raises(InvalidSelectorException):
289
driver.find_elements(By.CLASS_NAME, "")
290
291
292
def test_finding_asingle_element_by_compound_class_name_should_throw(driver, pages):
293
pages.load("xhtmlTest.html")
294
with pytest.raises(InvalidSelectorException):
295
driver.find_element(By.CLASS_NAME, "a b")
296
297
298
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
299
def test_finding_asingle_element_by_invalid_class_name_should_throw(driver, pages):
300
pages.load("xhtmlTest.html")
301
with pytest.raises(InvalidSelectorException):
302
driver.find_element(By.CLASS_NAME, "!@#$%^&*")
303
304
305
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
306
def test_finding_multiple_elements_by_invalid_class_name_should_throw(driver, pages):
307
pages.load("xhtmlTest.html")
308
with pytest.raises(InvalidSelectorException):
309
driver.find_elements(By.CLASS_NAME, "!@#$%^&*")
310
311
312
# By.XPATH positive
313
314
315
def test_should_be_able_to_find_asingle_element_by_xpath(driver, pages):
316
pages.load("xhtmlTest.html")
317
element = driver.find_element(By.XPATH, "//h1")
318
assert element.text == "XHTML Might Be The Future"
319
320
321
def test_should_be_able_to_find_multiple_elements_by_xpath(driver, pages):
322
pages.load("xhtmlTest.html")
323
elements = driver.find_elements(By.XPATH, "//div")
324
assert len(elements) == 13
325
326
327
def test_should_be_able_to_find_many_elements_repeatedly_by_xpath(driver, pages):
328
pages.load("xhtmlTest.html")
329
xpath = "//node()[contains(@id,'id')]"
330
assert len(driver.find_elements(By.XPATH, xpath)) == 3
331
332
xpath = "//node()[contains(@id,'nope')]"
333
assert len(driver.find_elements(By.XPATH, xpath)) == 0
334
335
336
def test_should_be_able_to_identify_elements_by_class(driver, pages):
337
pages.load("xhtmlTest.html")
338
header = driver.find_element(By.XPATH, "//h1[@class='header']")
339
assert header.text == "XHTML Might Be The Future"
340
341
342
def test_should_be_able_to_find_an_element_by_xpath_with_multiple_attributes(driver, pages):
343
pages.load("formPage.html")
344
element = driver.find_element(By.XPATH, "//form[@name='optional']/input[@type='submit' and @value='Click!']")
345
assert element.tag_name.lower() == "input"
346
assert element.get_attribute("value") == "Click!"
347
348
349
def test_finding_alink_by_xpath_should_locate_an_element_with_the_given_text(driver, pages):
350
pages.load("xhtmlTest.html")
351
element = driver.find_element(By.XPATH, "//a[text()='click me']")
352
assert element.text == "click me"
353
354
355
def test_finding_alink_by_xpath_using_contains_keyword_should_work(driver, pages):
356
pages.load("nestedElements.html")
357
element = driver.find_element(By.XPATH, "//a[contains(.,'hello world')]")
358
assert "hello world" in element.text
359
360
361
# @pytest.mark.xfail_chrome(raises=InvalidSelectorException)
362
# @pytest.mark.xfail_edge(raises=InvalidSelectorException)
363
# @pytest.mark.xfail_firefox(raises=InvalidSelectorException)
364
# @pytest.mark.xfail_remote(raises=InvalidSelectorException)
365
# @pytest.mark.xfail_safari(raises=NoSuchElementException)
366
# @pytest.mark.xfail_webkitgtk(raises=InvalidSelectorException)
367
# def test_Should_Be_Able_To_Find_Element_By_XPath_With_Namespace(driver, pages):
368
# pages.load("svgPage.html")
369
# element = driver.find_element(By.XPATH, "//svg:svg//svg:text")
370
# assert element.text == "Test Chart"
371
372
373
def test_should_be_able_to_find_element_by_xpath_in_xml_document(driver, pages):
374
pages.load("simple.xml")
375
element = driver.find_element(By.XPATH, "//foo")
376
assert "baz" in element.text
377
378
379
# By.XPATH negative
380
381
382
def test_should_throw_an_exception_when_there_is_no_link_to_click(driver, pages):
383
pages.load("xhtmlTest.html")
384
with pytest.raises(NoSuchElementException):
385
driver.find_element(By.XPATH, "//a[@id='Not here']")
386
387
388
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
389
def test_should_throw_invalid_selector_exception_when_xpath_is_syntactically_invalid_in_driver_find_element(
390
driver, pages
391
):
392
pages.load("formPage.html")
393
with pytest.raises(InvalidSelectorException):
394
driver.find_element(By.XPATH, "this][isnot][valid")
395
396
397
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
398
def test_should_throw_invalid_selector_exception_when_xpath_is_syntactically_invalid_in_driver_find_elements(
399
driver, pages
400
):
401
pages.load("formPage.html")
402
with pytest.raises(InvalidSelectorException):
403
driver.find_elements(By.XPATH, "this][isnot][valid")
404
405
406
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
407
def test_should_throw_invalid_selector_exception_when_xpath_is_syntactically_invalid_in_element_find_element(
408
driver, pages
409
):
410
pages.load("formPage.html")
411
body = driver.find_element(By.TAG_NAME, "body")
412
with pytest.raises(InvalidSelectorException):
413
body.find_element(By.XPATH, "this][isnot][valid")
414
415
416
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
417
def test_should_throw_invalid_selector_exception_when_xpath_is_syntactically_invalid_in_element_find_elements(
418
driver, pages
419
):
420
pages.load("formPage.html")
421
body = driver.find_element(By.TAG_NAME, "body")
422
with pytest.raises(InvalidSelectorException):
423
body.find_elements(By.XPATH, "this][isnot][valid")
424
425
426
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
427
def test_should_throw_invalid_selector_exception_when_xpath_returns_wrong_type_in_driver_find_element(driver, pages):
428
pages.load("formPage.html")
429
with pytest.raises(InvalidSelectorException):
430
driver.find_element(By.XPATH, "count(//input)")
431
432
433
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
434
def test_should_throw_invalid_selector_exception_when_xpath_returns_wrong_type_in_driver_find_elements(driver, pages):
435
pages.load("formPage.html")
436
with pytest.raises(InvalidSelectorException):
437
driver.find_elements(By.XPATH, "count(//input)")
438
439
440
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
441
def test_should_throw_invalid_selector_exception_when_xpath_returns_wrong_type_in_element_find_element(driver, pages):
442
pages.load("formPage.html")
443
body = driver.find_element(By.TAG_NAME, "body")
444
with pytest.raises(InvalidSelectorException):
445
body.find_element(By.XPATH, "count(//input)")
446
447
448
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
449
def test_should_throw_invalid_selector_exception_when_xpath_returns_wrong_type_in_element_find_elements(driver, pages):
450
pages.load("formPage.html")
451
body = driver.find_element(By.TAG_NAME, "body")
452
with pytest.raises(InvalidSelectorException):
453
body.find_elements(By.XPATH, "count(//input)")
454
455
456
# By.CSS_SELECTOR positive
457
458
459
def test_should_be_able_to_find_asingle_element_by_css_selector(driver, pages):
460
pages.load("xhtmlTest.html")
461
element = driver.find_element(By.CSS_SELECTOR, "div.content")
462
assert element.tag_name.lower() == "div"
463
assert element.get_attribute("class") == "content"
464
465
466
def test_should_be_able_to_find_multiple_elements_by_css_selector(driver, pages):
467
pages.load("xhtmlTest.html")
468
elements = driver.find_elements(By.CSS_SELECTOR, "p")
469
assert len(elements) > 1
470
471
472
def test_should_be_able_to_find_asingle_element_by_compound_css_selector(driver, pages):
473
pages.load("xhtmlTest.html")
474
element = driver.find_element(By.CSS_SELECTOR, "div.extraDiv, div.content")
475
assert element.tag_name.lower() == "div"
476
assert element.get_attribute("class") == "content"
477
478
479
def test_should_be_able_to_find_multiple_elements_by_compound_css_selector(driver, pages):
480
pages.load("xhtmlTest.html")
481
elements = driver.find_elements(By.CSS_SELECTOR, "div.extraDiv, div.content")
482
assert len(elements) > 1
483
assert elements[0].get_attribute("class") == "content"
484
assert elements[1].get_attribute("class") == "extraDiv"
485
486
487
def test_should_be_able_to_find_an_element_by_boolean_attribute_using_css_selector(driver, pages):
488
pages.load("locators_tests/boolean_attribute_selected.html")
489
element = driver.find_element(By.CSS_SELECTOR, "option[selected='selected']")
490
assert element.get_attribute("value") == "two"
491
492
493
def test_should_be_able_to_find_an_element_by_boolean_attribute_using_short_css_selector(driver, pages):
494
pages.load("locators_tests/boolean_attribute_selected.html")
495
element = driver.find_element(By.CSS_SELECTOR, "option[selected]")
496
assert element.get_attribute("value") == "two"
497
498
499
def test_should_be_able_to_find_an_element_by_boolean_attribute_using_short_css_selector_on_html_4_page(driver, pages):
500
pages.load("locators_tests/boolean_attribute_selected_html4.html")
501
element = driver.find_element(By.CSS_SELECTOR, "option[selected]")
502
assert element.get_attribute("value") == "two"
503
504
505
# By.CSS_SELECTOR negative
506
507
508
def test_should_not_find_element_by_css_selector_when_there_is_no_such_element(driver, pages):
509
pages.load("xhtmlTest.html")
510
with pytest.raises(NoSuchElementException):
511
driver.find_element(By.CSS_SELECTOR, ".there-is-no-such-class")
512
513
514
def test_should_not_find_elements_by_css_selector_when_there_is_no_such_element(driver, pages):
515
pages.load("xhtmlTest.html")
516
elements = driver.find_elements(By.CSS_SELECTOR, ".there-is-no-such-class")
517
assert len(elements) == 0
518
519
520
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
521
def test_finding_asingle_element_by_empty_css_selector_should_throw(driver, pages):
522
pages.load("xhtmlTest.html")
523
with pytest.raises(InvalidSelectorException):
524
driver.find_element(By.CSS_SELECTOR, "")
525
526
527
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
528
def test_finding_multiple_elements_by_empty_css_selector_should_throw(driver, pages):
529
pages.load("xhtmlTest.html")
530
with pytest.raises(InvalidSelectorException):
531
driver.find_elements(By.CSS_SELECTOR, "")
532
533
534
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
535
def test_finding_asingle_element_by_invalid_css_selector_should_throw(driver, pages):
536
pages.load("xhtmlTest.html")
537
with pytest.raises(InvalidSelectorException):
538
driver.find_element(By.CSS_SELECTOR, "//a/b/c[@id='1']")
539
540
541
@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")
542
def test_finding_multiple_elements_by_invalid_css_selector_should_throw(driver, pages):
543
pages.load("xhtmlTest.html")
544
with pytest.raises(InvalidSelectorException):
545
driver.find_elements(By.CSS_SELECTOR, "//a/b/c[@id='1']")
546
547
548
# By.LINK_TEXT positive
549
550
551
def test_should_be_able_to_find_alink_by_text(driver, pages):
552
pages.load("xhtmlTest.html")
553
link = driver.find_element(By.LINK_TEXT, "click me")
554
assert link.text == "click me"
555
556
557
def test_should_be_able_to_find_multiple_links_by_text(driver, pages):
558
pages.load("xhtmlTest.html")
559
elements = driver.find_elements(By.LINK_TEXT, "click me")
560
assert len(elements) == 2
561
562
563
def test_should_find_element_by_link_text_containing_equals_sign(driver, pages):
564
pages.load("xhtmlTest.html")
565
element = driver.find_element(By.LINK_TEXT, "Link=equalssign")
566
assert element.get_attribute("id") == "linkWithEqualsSign"
567
568
569
def test_should_find_multiple_elements_by_link_text_containing_equals_sign(driver, pages):
570
pages.load("xhtmlTest.html")
571
elements = driver.find_elements(By.LINK_TEXT, "Link=equalssign")
572
assert 1 == len(elements)
573
assert elements[0].get_attribute("id") == "linkWithEqualsSign"
574
575
576
def test_finds_by_link_text_on_xhtml_page(driver, pages):
577
pages.load("actualXhtmlPage.xhtml")
578
link_Text = "Foo"
579
element = driver.find_element(By.LINK_TEXT, link_Text)
580
assert element.text == link_Text
581
582
583
def test_link_with_formatting_tags(driver, pages):
584
pages.load("simpleTest.html")
585
elem = driver.find_element(By.ID, "links")
586
587
res = elem.find_element(By.PARTIAL_LINK_TEXT, "link with formatting tags")
588
assert res.text == "link with formatting tags"
589
590
591
@pytest.mark.xfail_safari
592
def test_driver_can_get_link_by_link_test_ignoring_trailing_whitespace(driver, pages):
593
pages.load("simpleTest.html")
594
link = driver.find_element(By.LINK_TEXT, "link with trailing space")
595
assert link.get_attribute("id") == "linkWithTrailingSpace"
596
assert link.text == "link with trailing space"
597
598
599
# By.LINK_TEXT negative
600
601
602
def test_should_not_be_able_to_locate_by_link_text_asingle_element_that_does_not_exist(driver, pages):
603
pages.load("xhtmlTest.html")
604
with pytest.raises(NoSuchElementException):
605
driver.find_element(By.LINK_TEXT, "Not here either")
606
607
608
def test_should_not_be_able_to_locate_by_link_text_multiple_elements_that_do_not_exist(driver, pages):
609
pages.load("xhtmlTest.html")
610
elements = driver.find_elements(By.LINK_TEXT, "Not here either")
611
assert len(elements) == 0
612
613
614
# By.PARTIAL_LINK_TEXT positive
615
616
617
def test_should_be_able_to_find_multiple_elements_by_partial_link_text(driver, pages):
618
pages.load("xhtmlTest.html")
619
elements = driver.find_elements(By.PARTIAL_LINK_TEXT, "ick me")
620
assert len(elements) == 2
621
622
623
def test_should_be_able_to_find_asingle_element_by_partial_link_text(driver, pages):
624
pages.load("xhtmlTest.html")
625
element = driver.find_element(By.PARTIAL_LINK_TEXT, "anon")
626
assert "anon" in element.text
627
628
629
def test_should_find_element_by_partial_link_text_containing_equals_sign(driver, pages):
630
pages.load("xhtmlTest.html")
631
element = driver.find_element(By.PARTIAL_LINK_TEXT, "Link=")
632
assert element.get_attribute("id") == "linkWithEqualsSign"
633
634
635
def test_should_find_multiple_elements_by_partial_link_text_containing_equals_sign(driver, pages):
636
pages.load("xhtmlTest.html")
637
elements = driver.find_elements(By.PARTIAL_LINK_TEXT, "Link=")
638
assert len(elements) == 1
639
assert elements[0].get_attribute("id") == "linkWithEqualsSign"
640
641
642
# Misc tests
643
644
645
def test_driver_should_be_able_to_find_elements_after_loading_more_than_one_page_at_atime(driver, pages):
646
pages.load("formPage.html")
647
pages.load("xhtmlTest.html")
648
link = driver.find_element(By.LINK_TEXT, "click me")
649
assert link.text == "click me"
650
651
652
# You don't want to ask why this is here
653
654
655
def test_when_finding_by_name_should_not_return_by_id(driver, pages):
656
pages.load("formPage.html")
657
658
element = driver.find_element(By.NAME, "id-name1")
659
assert element.get_attribute("value") == "name"
660
661
element = driver.find_element(By.ID, "id-name1")
662
assert element.get_attribute("value") == "id"
663
664
element = driver.find_element(By.NAME, "id-name2")
665
assert element.get_attribute("value") == "name"
666
667
element = driver.find_element(By.ID, "id-name2")
668
assert element.get_attribute("value") == "id"
669
670
671
def test_should_be_able_to_find_ahidden_elements_by_name(driver, pages):
672
pages.load("formPage.html")
673
element = driver.find_element(By.NAME, "hidden")
674
assert element.get_attribute("name") == "hidden"
675
676
677
def test_should_not_be_able_to_find_an_element_on_a_blank_page(driver, pages):
678
driver.get("about:blank")
679
with pytest.raises(NoSuchElementException):
680
driver.find_element(By.TAG_NAME, "a")
681
682
683
# custom finders tests
684
685
686
def test_register_and_get_custom_finder():
687
By.register_custom_finder("custom", "custom strategy")
688
assert By.get_finder("custom") == "custom strategy"
689
690
691
def test_get_nonexistent_finder():
692
assert By.get_finder("nonexistent") is None
693
694
695
def test_clear_custom_finders():
696
By.register_custom_finder("custom", "custom strategy")
697
By.clear_custom_finders()
698
assert By.get_finder("custom") is None
699
700