@@ -947,6 +947,7 @@ def find_elements(self, selector, by=By.CSS_SELECTOR, limit=0):
947
947
""" Returns a list of matching WebElements.
948
948
If "limit" is set and > 0, will only return that many elements. """
949
949
self .wait_for_ready_state_complete ()
950
+ time .sleep (0.05 )
950
951
selector , by = self .__recalculate_selector (selector , by )
951
952
elements = self .driver .find_elements (by = by , value = selector )
952
953
if limit and limit > 0 and len (elements ) > limit :
@@ -957,6 +958,7 @@ def find_visible_elements(self, selector, by=By.CSS_SELECTOR, limit=0):
957
958
""" Returns a list of matching WebElements that are visible.
958
959
If "limit" is set and > 0, will only return that many elements. """
959
960
self .wait_for_ready_state_complete ()
961
+ time .sleep (0.05 )
960
962
selector , by = self .__recalculate_selector (selector , by )
961
963
v_elems = page_actions .find_visible_elements (self .driver , selector , by )
962
964
if limit and limit > 0 and len (v_elems ) > limit :
@@ -999,6 +1001,28 @@ def click_visible_elements(self, selector, by=By.CSS_SELECTOR, limit=0):
999
1001
except (StaleElementReferenceException , ENI_Exception ):
1000
1002
return # Probably on new page / Elements are all stale
1001
1003
1004
+ def click_nth_visible_element (self , selector , number , by = By .CSS_SELECTOR ):
1005
+ """ Finds all matching page elements and clicks the nth visible one.
1006
+ Example: self.click_nth_visible_element('[type="checkbox"]', 5)
1007
+ (Clicks the 5th visible checkbox on the page.) """
1008
+ elements = self .find_visible_elements (selector , by = by )
1009
+ if len (elements ) < number :
1010
+ raise Exception ("Not enough matching {%s} elements of type {%s} to"
1011
+ " click number %s!" % (selector , by , number ))
1012
+ number = number - 1
1013
+ if number < 0 :
1014
+ number = 0
1015
+ element = elements [number ]
1016
+ self .wait_for_ready_state_complete ()
1017
+ try :
1018
+ self .__scroll_to_element (element )
1019
+ element .click ()
1020
+ except (StaleElementReferenceException , ENI_Exception ):
1021
+ self .wait_for_ready_state_complete ()
1022
+ time .sleep (0.05 )
1023
+ self .__scroll_to_element (element )
1024
+ element .click ()
1025
+
1002
1026
def click_if_visible (self , selector , by = By .CSS_SELECTOR ):
1003
1027
""" If the page selector exists and is visible, clicks on the element.
1004
1028
This method only clicks on the first matching element found.
0 commit comments