22import time
33from selenium .webdriver import ActionChains
44from selenium .webdriver .common .by import By
5+ from selenium .common .exceptions import WebDriverException
56from selenium .webdriver .common .keys import Keys
67from selenium .webdriver .support .ui import WebDriverWait
78from selenium .webdriver .support import expected_conditions as EC
1213pjoin = os .path .join
1314
1415
15- def wait_for_selector (driver , selector , timeout = 10 , visible = False , single = False ):
16+ def wait_for_selector (driver , selector , timeout = 10 , visible = False , single = False , wait_for_n = 1 ):
17+ if wait_for_n > 1 :
18+ return _wait_for_multiple (
19+ driver , By .CSS_SELECTOR , selector , timeout , wait_for_n , visible )
1620 return _wait_for (driver , By .CSS_SELECTOR , selector , timeout , visible , single )
1721
18- def wait_for_tag (driver , tag , timeout = 10 , visible = False , single = False ):
22+
23+ def wait_for_tag (driver , tag , timeout = 10 , visible = False , single = False , wait_for_n = 1 ):
24+ if wait_for_n > 1 :
25+ return _wait_for_multiple (
26+ driver , By .TAG_NAME , tag , timeout , wait_for_n , visible )
1927 return _wait_for (driver , By .TAG_NAME , tag , timeout , visible , single )
2028
29+
2130def _wait_for (driver , locator_type , locator , timeout = 10 , visible = False , single = False ):
31+ """Waits `timeout` seconds for the specified condition to be met. Condition is
32+ met if any matching element is found. Returns located element(s) when found.
33+
34+ Args:
35+ driver: Selenium web driver instance
36+ locator_type: type of locator (e.g. By.CSS_SELECTOR or By.TAG_NAME)
37+ locator: name of tag, class, etc. to wait for
38+ timeout: how long to wait for presence/visibility of element
39+ visible: if True, require that element is not only present, but visible
40+ single: if True, return a single element, otherwise return a list of matching
41+ elements
42+ """
2243 wait = WebDriverWait (driver , timeout )
2344 if single :
2445 if visible :
@@ -33,6 +54,31 @@ def _wait_for(driver, locator_type, locator, timeout=10, visible=False, single=F
3354 return wait .until (conditional ((locator_type , locator )))
3455
3556
57+ def _wait_for_multiple (driver , locator_type , locator , timeout , wait_for_n , visible = False ):
58+ """Waits until `wait_for_n` matching elements to be present (or visible).
59+ Returns located elements when found.
60+
61+ Args:
62+ driver: Selenium web driver instance
63+ locator_type: type of locator (e.g. By.CSS_SELECTOR or By.TAG_NAME)
64+ locator: name of tag, class, etc. to wait for
65+ timeout: how long to wait for presence/visibility of element
66+ wait_for_n: wait until this number of matching elements are present/visible
67+ visible: if True, require that elements are not only present, but visible
68+ """
69+ wait = WebDriverWait (driver , timeout )
70+
71+ def multiple_found (driver ):
72+ elements = driver .find_elements (locator_type , locator )
73+ if visible :
74+ elements = [e for e in elements if e .is_displayed ()]
75+ if len (elements ) < wait_for_n :
76+ return False
77+ return elements
78+
79+ return wait .until (multiple_found )
80+
81+
3682class CellTypeError (ValueError ):
3783
3884 def __init__ (self , message = "" ):
0 commit comments