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