Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/support/expected_conditions.py
4037 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 re
19
from collections.abc import Callable, Iterable
20
from typing import Any, Literal, TypeVar
21
22
from selenium.common.exceptions import (
23
NoAlertPresentException,
24
NoSuchElementException,
25
NoSuchFrameException,
26
StaleElementReferenceException,
27
WebDriverException,
28
)
29
from selenium.webdriver.common.alert import Alert
30
from selenium.webdriver.remote.webdriver import WebDriver, WebElement
31
32
"""
33
* Canned "Expected Conditions" which are generally useful within webdriver
34
* tests.
35
"""
36
37
D = TypeVar("D")
38
T = TypeVar("T")
39
40
WebDriverOrWebElement = WebDriver | WebElement
41
42
43
def title_is(title: str) -> Callable[[WebDriver], bool]:
44
"""An expectation for checking the title of a page.
45
46
Args:
47
title: The expected title, which must be an exact match.
48
49
Returns:
50
True if the title matches, False otherwise.
51
"""
52
53
def _predicate(driver: WebDriver):
54
return driver.title == title
55
56
return _predicate
57
58
59
def title_contains(title: str) -> Callable[[WebDriver], bool]:
60
"""Check that the title contains a case-sensitive substring.
61
62
Args:
63
title: The fragment of title expected.
64
65
Returns:
66
True when the title matches, False otherwise.
67
"""
68
69
def _predicate(driver: WebDriver):
70
return title in driver.title
71
72
return _predicate
73
74
75
def presence_of_element_located(locator: tuple[str, str]) -> Callable[[WebDriverOrWebElement], WebElement]:
76
"""Check that an element is present on the DOM (not necessarily visible).
77
78
Args:
79
locator: Used to find the element.
80
81
Returns:
82
The WebElement once it is located.
83
84
Example:
85
from selenium.webdriver.common.by import By
86
from selenium.webdriver.support.ui import WebDriverWait
87
from selenium.webdriver.support import expected_conditions as EC
88
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "q")))
89
"""
90
91
def _predicate(driver: WebDriverOrWebElement):
92
return driver.find_element(*locator)
93
94
return _predicate
95
96
97
def url_contains(url: str) -> Callable[[WebDriver], bool]:
98
"""Check that the current url contains a case-sensitive substring.
99
100
Args:
101
url: The fragment of url expected.
102
103
Returns:
104
True when the url matches, False otherwise.
105
"""
106
107
def _predicate(driver: WebDriver):
108
return url in driver.current_url
109
110
return _predicate
111
112
113
def url_matches(pattern: str) -> Callable[[WebDriver], bool]:
114
"""An expectation for checking the current url.
115
116
Args:
117
pattern: The pattern to match with the current url.
118
119
Returns:
120
True when the pattern matches, False otherwise.
121
122
Note:
123
More powerful than url_contains, as it allows for regular expressions.
124
"""
125
126
def _predicate(driver: WebDriver):
127
return re.search(pattern, driver.current_url) is not None
128
129
return _predicate
130
131
132
def url_to_be(url: str) -> Callable[[WebDriver], bool]:
133
"""An expectation for checking the current url.
134
135
Args:
136
url: The expected url, which must be an exact match.
137
138
Returns:
139
True when the url matches, False otherwise.
140
"""
141
142
def _predicate(driver: WebDriver):
143
return url == driver.current_url
144
145
return _predicate
146
147
148
def url_changes(url: str) -> Callable[[WebDriver], bool]:
149
"""Check that the current url differs from a given string.
150
151
Args:
152
url: The expected url, which must not be an exact match.
153
154
Returns:
155
True when the url does not match, False otherwise.
156
"""
157
158
def _predicate(driver: WebDriver):
159
return url != driver.current_url
160
161
return _predicate
162
163
164
def visibility_of_element_located(
165
locator: tuple[str, str],
166
) -> Callable[[WebDriverOrWebElement], Literal[False] | WebElement]:
167
"""Check that an element is visible (present in DOM and width/height greater than zero).
168
169
Args:
170
locator: Used to find the element.
171
172
Returns:
173
The WebElement once it is located and visible.
174
175
Example:
176
from selenium.webdriver.common.by import By
177
from selenium.webdriver.support.ui import WebDriverWait
178
from selenium.webdriver.support import expected_conditions as EC
179
element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.NAME, "q")))
180
"""
181
182
def _predicate(driver: WebDriverOrWebElement):
183
try:
184
return _element_if_visible(driver.find_element(*locator))
185
except StaleElementReferenceException:
186
return False
187
188
return _predicate
189
190
191
def visibility_of(element: WebElement) -> Callable[[Any], Literal[False] | WebElement]:
192
"""Check that an element is visible (present in DOM and width/height greater than zero).
193
194
Args:
195
element: The WebElement to check.
196
197
Returns:
198
The WebElement once it is visible.
199
200
Example:
201
from selenium.webdriver.common.by import By
202
from selenium.webdriver.support.ui import WebDriverWait
203
from selenium.webdriver.support import expected_conditions as EC
204
element = WebDriverWait(driver, 10).until(EC.visibility_of(driver.find_element(By.NAME, "q")))
205
"""
206
207
def _predicate(_):
208
return _element_if_visible(element)
209
210
return _predicate
211
212
213
def _element_if_visible(element: WebElement, visibility: bool = True) -> Literal[False] | WebElement:
214
"""Check if an element has the expected visibility state.
215
216
Args:
217
element: The WebElement to check.
218
visibility: The expected visibility of the element.
219
220
Returns:
221
The WebElement once it is visible or not visible.
222
"""
223
return element if element.is_displayed() == visibility else False
224
225
226
def presence_of_all_elements_located(locator: tuple[str, str]) -> Callable[[WebDriverOrWebElement], list[WebElement]]:
227
"""Check that all elements matching the locator are present on the DOM.
228
229
Args:
230
locator: Used to find the element.
231
232
Returns:
233
The list of WebElements once they are located.
234
235
Example:
236
from selenium.webdriver.common.by import By
237
from selenium.webdriver.support.ui import WebDriverWait
238
from selenium.webdriver.support import expected_conditions as EC
239
elements = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "foo")))
240
"""
241
242
def _predicate(driver: WebDriverOrWebElement):
243
return driver.find_elements(*locator)
244
245
return _predicate
246
247
248
def visibility_of_any_elements_located(locator: tuple[str, str]) -> Callable[[WebDriverOrWebElement], list[WebElement]]:
249
"""Check that at least one element is visible on the web page (present in DOM and width/height greater than zero).
250
251
Args:
252
locator: Used to find the element.
253
254
Returns:
255
The list of WebElements once they are located and visible.
256
257
Example:
258
from selenium.webdriver.common.by import By
259
from selenium.webdriver.support.ui import WebDriverWait
260
from selenium.webdriver.support import expected_conditions as EC
261
elements = WebDriverWait(driver, 10).until(EC.visibility_of_any_elements_located((By.CLASS_NAME, "foo")))
262
"""
263
264
def _predicate(driver: WebDriverOrWebElement):
265
return [element for element in driver.find_elements(*locator) if _element_if_visible(element)]
266
267
return _predicate
268
269
270
def visibility_of_all_elements_located(
271
locator: tuple[str, str],
272
) -> Callable[[WebDriverOrWebElement], list[WebElement] | Literal[False]]:
273
"""Check that all elements are visible (present in DOM and width/height greater than zero).
274
275
Args:
276
locator: Used to find the elements.
277
278
Returns:
279
The list of WebElements once they are located and visible.
280
281
Example:
282
from selenium.webdriver.common.by import By
283
from selenium.webdriver.support.ui import WebDriverWait
284
from selenium.webdriver.support import expected_conditions as EC
285
elements = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "foo")))
286
"""
287
288
def _predicate(driver: WebDriverOrWebElement):
289
try:
290
elements = driver.find_elements(*locator)
291
for element in elements:
292
if _element_if_visible(element, visibility=False):
293
return False
294
return elements
295
except StaleElementReferenceException:
296
return False
297
298
return _predicate
299
300
301
def text_to_be_present_in_element(locator: tuple[str, str], text_: str) -> Callable[[WebDriverOrWebElement], bool]:
302
"""Check that the given text is present in the specified element.
303
304
Args:
305
locator: Used to find the element.
306
text_: The text to be present in the element.
307
308
Returns:
309
True when the text is present, False otherwise.
310
311
Example:
312
from selenium.webdriver.common.by import By
313
from selenium.webdriver.support.ui import WebDriverWait
314
from selenium.webdriver.support import expected_conditions as EC
315
is_text_in_element = WebDriverWait(driver, 10).until(
316
EC.text_to_be_present_in_element((By.CLASS_NAME, "foo"), "bar")
317
)
318
"""
319
320
def _predicate(driver: WebDriverOrWebElement):
321
try:
322
element_text = driver.find_element(*locator).text
323
return text_ in element_text
324
except StaleElementReferenceException:
325
return False
326
327
return _predicate
328
329
330
def text_to_be_present_in_element_value(
331
locator: tuple[str, str], text_: str
332
) -> Callable[[WebDriverOrWebElement], bool]:
333
"""Check that the given text is present in the element's value.
334
335
Args:
336
locator: Used to find the element.
337
text_: The text to be present in the element's value.
338
339
Returns:
340
True when the text is present, False otherwise.
341
342
Example:
343
from selenium.webdriver.common.by import By
344
from selenium.webdriver.support.ui import WebDriverWait
345
from selenium.webdriver.support import expected_conditions as EC
346
is_text_in_element_value = WebDriverWait(driver, 10).until(
347
EC.text_to_be_present_in_element_value((By.CLASS_NAME, "foo"), "bar")
348
)
349
"""
350
351
def _predicate(driver: WebDriverOrWebElement):
352
try:
353
element_text = driver.find_element(*locator).get_attribute("value")
354
if element_text is None:
355
return False
356
return text_ in element_text
357
except StaleElementReferenceException:
358
return False
359
360
return _predicate
361
362
363
def text_to_be_present_in_element_attribute(
364
locator: tuple[str, str], attribute_: str, text_: str
365
) -> Callable[[WebDriverOrWebElement], bool]:
366
"""Check that the given text is present in the element's attribute.
367
368
Args:
369
locator: Used to find the element.
370
attribute_: The attribute to check the text in.
371
text_: The text to be present in the element's attribute.
372
373
Returns:
374
True when the text is present, False otherwise.
375
376
Example:
377
from selenium.webdriver.common.by import By
378
from selenium.webdriver.support.ui import WebDriverWait
379
from selenium.webdriver.support import expected_conditions as EC
380
is_text_in_element_attribute = WebDriverWait(driver, 10).until(
381
EC.text_to_be_present_in_element_attribute((By.CLASS_NAME, "foo"), "bar", "baz")
382
)
383
"""
384
385
def _predicate(driver: WebDriverOrWebElement):
386
try:
387
element_text = driver.find_element(*locator).get_attribute(attribute_)
388
if element_text is None:
389
return False
390
return text_ in element_text
391
except StaleElementReferenceException:
392
return False
393
394
return _predicate
395
396
397
def frame_to_be_available_and_switch_to_it(
398
locator: tuple[str, str] | str | WebElement,
399
) -> Callable[[WebDriver], bool]:
400
"""Check that the given frame is available and switch to it.
401
402
Args:
403
locator: Used to find the frame.
404
405
Returns:
406
True when the frame is available, False otherwise.
407
408
Example:
409
from selenium.webdriver.support.ui import WebDriverWait
410
from selenium.webdriver.support import expected_conditions as EC
411
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("frame_name"))
412
"""
413
414
def _predicate(driver: WebDriver):
415
try:
416
if isinstance(locator, Iterable) and not isinstance(locator, str):
417
driver.switch_to.frame(driver.find_element(*locator))
418
else:
419
driver.switch_to.frame(locator)
420
return True
421
except NoSuchFrameException:
422
return False
423
424
return _predicate
425
426
427
def invisibility_of_element_located(
428
locator: WebElement | tuple[str, str],
429
) -> Callable[[WebDriverOrWebElement], WebElement | bool]:
430
"""Check that an element is either invisible or not present on the DOM.
431
432
Args:
433
locator: Used to find the element.
434
435
Returns:
436
True when the element is invisible or not present, False otherwise.
437
438
Example:
439
from selenium.webdriver.common.by import By
440
from selenium.webdriver.support.ui import WebDriverWait
441
from selenium.webdriver.support import expected_conditions as EC
442
is_invisible = WebDriverWait(driver, 10).until(EC.invisibility_of_element_located((By.CLASS_NAME, "foo")))
443
444
Note:
445
In the case of NoSuchElement, returns true because the element is not
446
present in DOM. The try block checks if the element is present but is
447
invisible.
448
In the case of StaleElementReference, returns true because stale element
449
reference implies that element is no longer visible.
450
"""
451
452
def _predicate(driver: WebDriverOrWebElement):
453
try:
454
target = locator
455
if not isinstance(target, WebElement):
456
target = driver.find_element(*target)
457
return _element_if_visible(target, visibility=False)
458
except (NoSuchElementException, StaleElementReferenceException):
459
# In the case of NoSuchElement, returns true because the element is
460
# not present in DOM. The try block checks if the element is present
461
# but is invisible.
462
# In the case of StaleElementReference, returns true because stale
463
# element reference implies that element is no longer visible.
464
return True
465
466
return _predicate
467
468
469
def invisibility_of_element(
470
element: WebElement | tuple[str, str],
471
) -> Callable[[WebDriverOrWebElement], WebElement | bool]:
472
"""Check that an element is either invisible or not present on the DOM.
473
474
Args:
475
element: Used to find the element.
476
477
Returns:
478
True when the element is invisible or not present, False otherwise.
479
480
Example:
481
from selenium.webdriver.common.by import By
482
from selenium.webdriver.support.ui import WebDriverWait
483
from selenium.webdriver.support import expected_conditions as EC
484
is_invisible_or_not_present = WebDriverWait(driver, 10).until(
485
EC.invisibility_of_element(driver.find_element(By.CLASS_NAME, "foo"))
486
)
487
"""
488
return invisibility_of_element_located(element)
489
490
491
def element_to_be_clickable(
492
mark: WebElement | tuple[str, str],
493
) -> Callable[[WebDriverOrWebElement], Literal[False] | WebElement]:
494
"""Check that an element is visible and enabled so it can be clicked.
495
496
Args:
497
mark: Used to find the element.
498
499
Returns:
500
The WebElement once it is located and clickable.
501
502
Example:
503
from selenium.webdriver.common.by import By
504
from selenium.webdriver.support.ui import WebDriverWait
505
from selenium.webdriver.support import expected_conditions as EC
506
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "foo")))
507
"""
508
509
# renamed argument to 'mark', to indicate that both locator
510
# and WebElement args are valid
511
def _predicate(driver: WebDriverOrWebElement):
512
target = mark
513
if not isinstance(target, WebElement): # if given locator instead of WebElement
514
target = driver.find_element(*target) # grab element at locator
515
element = visibility_of(target)(driver)
516
if element and element.is_enabled():
517
return element
518
return False
519
520
return _predicate
521
522
523
def staleness_of(element: WebElement) -> Callable[[Any], bool]:
524
"""Wait until an element is no longer attached to the DOM.
525
526
Args:
527
element: The element to wait for.
528
529
Returns:
530
False if the element is still attached to the DOM, true otherwise.
531
532
Example:
533
from selenium.webdriver.common.by import By
534
from selenium.webdriver.support.ui import WebDriverWait
535
from selenium.webdriver.support import expected_conditions as EC
536
is_stale = WebDriverWait(driver, 10).until(EC.staleness_of(driver.find_element(By.CLASS_NAME, "foo")))
537
"""
538
539
def _predicate(_):
540
try:
541
# Calling any method forces a staleness check
542
element.is_enabled()
543
return False
544
except StaleElementReferenceException:
545
return True
546
547
return _predicate
548
549
550
def element_to_be_selected(element: WebElement) -> Callable[[Any], bool]:
551
"""An expectation for checking the selection is selected.
552
553
Args:
554
element: The WebElement to check.
555
556
Returns:
557
True if the element is selected, False otherwise.
558
559
Example:
560
from selenium.webdriver.common.by import By
561
from selenium.webdriver.support.ui import WebDriverWait
562
from selenium.webdriver.support import expected_conditions as EC
563
is_selected = WebDriverWait(driver, 10).until(EC.element_to_be_selected(driver.find_element(
564
By.CLASS_NAME, "foo"))
565
)
566
"""
567
568
def _predicate(_):
569
return element.is_selected()
570
571
return _predicate
572
573
574
def element_located_to_be_selected(locator: tuple[str, str]) -> Callable[[WebDriverOrWebElement], bool]:
575
"""An expectation for the element to be located is selected.
576
577
Args:
578
locator: Used to find the element.
579
580
Returns:
581
True if the element is selected, False otherwise.
582
583
Example:
584
from selenium.webdriver.common.by import By
585
from selenium.webdriver.support.ui import WebDriverWait
586
from selenium.webdriver.support import expected_conditions as EC
587
is_selected = WebDriverWait(driver, 10).until(EC.element_located_to_be_selected((By.CLASS_NAME, "foo")))
588
"""
589
590
def _predicate(driver: WebDriverOrWebElement):
591
return driver.find_element(*locator).is_selected()
592
593
return _predicate
594
595
596
def element_selection_state_to_be(element: WebElement, is_selected: bool) -> Callable[[Any], bool]:
597
"""An expectation for checking if the given element is selected.
598
599
Args:
600
element: The WebElement to check.
601
is_selected: The expected selection state.
602
603
Returns:
604
True if the element's selection state is the same as is_selected.
605
606
Example:
607
from selenium.webdriver.common.by import By
608
from selenium.webdriver.support.ui import WebDriverWait
609
from selenium.webdriver.support import expected_conditions as EC
610
is_selected = WebDriverWait(driver, 10).until(
611
EC.element_selection_state_to_be(driver.find_element(By.CLASS_NAME, "foo"), True)
612
)
613
"""
614
615
def _predicate(_):
616
return element.is_selected() == is_selected
617
618
return _predicate
619
620
621
def element_located_selection_state_to_be(
622
locator: tuple[str, str], is_selected: bool
623
) -> Callable[[WebDriverOrWebElement], bool]:
624
"""Check that an element's selection state matches the expected state.
625
626
Args:
627
locator: Used to find the element.
628
is_selected: The expected selection state.
629
630
Returns:
631
True if the element's selection state is the same as is_selected.
632
633
Example:
634
from selenium.webdriver.common.by import By
635
from selenium.webdriver.support.ui import WebDriverWait
636
from selenium.webdriver.support import expected_conditions as EC
637
is_selected = WebDriverWait(driver, 10).until(EC.element_located_selection_state_to_be(
638
(By.CLASS_NAME, "foo"), True)
639
)
640
"""
641
642
def _predicate(driver: WebDriverOrWebElement):
643
try:
644
element = driver.find_element(*locator)
645
return element.is_selected() == is_selected
646
except StaleElementReferenceException:
647
return False
648
649
return _predicate
650
651
652
def number_of_windows_to_be(num_windows: int) -> Callable[[WebDriver], bool]:
653
"""An expectation for the number of windows to be a certain value.
654
655
Args:
656
num_windows: The expected number of windows.
657
658
Returns:
659
True when the number of windows matches, False otherwise.
660
661
Example:
662
from selenium.webdriver.support.ui import WebDriverWait
663
from selenium.webdriver.support import expected_conditions as EC
664
is_number_of_windows = WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
665
"""
666
667
def _predicate(driver: WebDriver):
668
return len(driver.window_handles) == num_windows
669
670
return _predicate
671
672
673
def new_window_is_opened(current_handles: set[str]) -> Callable[[WebDriver], bool]:
674
"""Check that a new window has been opened (window handles count increased).
675
676
Args:
677
current_handles: The current window handles.
678
679
Returns:
680
True when a new window is opened, False otherwise.
681
682
Example:
683
from selenium.webdriver.support.ui import By
684
from selenium.webdriver.support.ui import WebDriverWait
685
from selenium.webdriver.support import expected_conditions as EC
686
is_new_window_opened = WebDriverWait(driver, 10).until(EC.new_window_is_opened(driver.window_handles))
687
"""
688
689
def _predicate(driver: WebDriver):
690
return len(driver.window_handles) > len(current_handles)
691
692
return _predicate
693
694
695
def alert_is_present() -> Callable[[WebDriver], Alert | Literal[False]]:
696
"""Check that an alert is present and switch to it.
697
698
Returns:
699
The Alert once it is located, or False if no alert is present.
700
701
Example:
702
from selenium.webdriver.support.ui import WebDriverWait
703
from selenium.webdriver.support import expected_conditions as EC
704
alert = WebDriverWait(driver, 10).until(EC.alert_is_present())
705
"""
706
707
def _predicate(driver: WebDriver):
708
try:
709
return driver.switch_to.alert
710
except NoAlertPresentException:
711
return False
712
713
return _predicate
714
715
716
def element_attribute_to_include(locator: tuple[str, str], attribute_: str) -> Callable[[WebDriverOrWebElement], bool]:
717
"""Check if the given attribute is included in the specified element.
718
719
Args:
720
locator: Used to find the element.
721
attribute_: The attribute to check.
722
723
Returns:
724
True when the attribute is included, False otherwise.
725
726
Example:
727
from selenium.webdriver.common.by import By
728
from selenium.webdriver.support.ui import WebDriverWait
729
from selenium.webdriver.support import expected_conditions as EC
730
is_attribute_in_element = WebDriverWait(driver, 10).until(
731
EC.element_attribute_to_include((By.CLASS_NAME, "foo"), "bar")
732
)
733
"""
734
735
def _predicate(driver: WebDriverOrWebElement):
736
try:
737
element_attribute = driver.find_element(*locator).get_attribute(attribute_)
738
return element_attribute is not None
739
except StaleElementReferenceException:
740
return False
741
742
return _predicate
743
744
745
def any_of(*expected_conditions: Callable[[D], T]) -> Callable[[D], Literal[False] | T]:
746
"""An expectation that any of multiple expected conditions is true.
747
748
Equivalent to a logical 'OR'. Returns results of the first matching
749
condition, or False if none do.
750
751
Args:
752
expected_conditions: The list of expected conditions to check.
753
754
Returns:
755
The result of the first matching condition, or False if none do.
756
757
Example:
758
from selenium.webdriver.common.by import By
759
from selenium.webdriver.support.ui import WebDriverWait
760
from selenium.webdriver.support import expected_conditions as EC
761
element = WebDriverWait(driver, 10).until(
762
EC.any_of(EC.presence_of_element_located((By.NAME, "q"),
763
EC.visibility_of_element_located((By.NAME, "q")))
764
)
765
"""
766
767
def any_of_condition(driver: D):
768
for expected_condition in expected_conditions:
769
try:
770
result = expected_condition(driver)
771
if result:
772
return result
773
except WebDriverException:
774
pass
775
return False
776
777
return any_of_condition
778
779
780
def all_of(
781
*expected_conditions: Callable[[D], T | Literal[False]],
782
) -> Callable[[D], list[T] | Literal[False]]:
783
"""An expectation that all of multiple expected conditions is true.
784
785
Equivalent to a logical 'AND'. When any ExpectedCondition is not met,
786
returns False. When all ExpectedConditions are met, returns a List with
787
each ExpectedCondition's return value.
788
789
Args:
790
expected_conditions: The list of expected conditions to check.
791
792
Returns:
793
The results of all the matching conditions, or False if any do not.
794
795
Example:
796
from selenium.webdriver.common.by import By
797
from selenium.webdriver.support.ui import WebDriverWait
798
from selenium.webdriver.support import expected_conditions as EC
799
elements = WebDriverWait(driver, 10).until(
800
EC.all_of(EC.presence_of_element_located((By.NAME, "q"),
801
EC.visibility_of_element_located((By.NAME, "q")))
802
)
803
"""
804
805
def all_of_condition(driver: D):
806
results: list[T] = []
807
for expected_condition in expected_conditions:
808
try:
809
result = expected_condition(driver)
810
if not result:
811
return False
812
results.append(result)
813
except WebDriverException:
814
return False
815
return results
816
817
return all_of_condition
818
819
820
def none_of(*expected_conditions: Callable[[D], Any]) -> Callable[[D], bool]:
821
"""An expectation that none of 1 or multiple expected conditions is true.
822
823
Equivalent to a logical 'NOT-OR'.
824
825
Args:
826
expected_conditions: The list of expected conditions to check.
827
828
Returns:
829
True if none of the conditions are true, False otherwise.
830
831
Example:
832
from selenium.webdriver.common.by import By
833
from selenium.webdriver.support.ui import WebDriverWait
834
from selenium.webdriver.support import expected_conditions as EC
835
element = WebDriverWait(driver, 10).until(
836
EC.none_of(EC.presence_of_element_located((By.NAME, "q"),
837
EC.visibility_of_element_located((By.NAME, "q")))
838
)
839
"""
840
841
def none_of_condition(driver: D):
842
for expected_condition in expected_conditions:
843
try:
844
result = expected_condition(driver)
845
if result:
846
return False
847
except WebDriverException:
848
pass
849
return True
850
851
return none_of_condition
852
853