@@ -663,6 +663,36 @@ def find_visible_elements(self, selector, by=By.CSS_SELECTOR):
663663 by = By .LINK_TEXT
664664 return page_actions .find_visible_elements (self .driver , selector , by )
665665
666+ def click_visible_elements (self , selector , by = By .CSS_SELECTOR ):
667+ """ Finds all matching page elements and clicks visible ones in order.
668+ If a click reloads or opens a new page, the clicking will stop.
669+ Works best for actions such as clicking all checkboxes on a page.
670+ Example: self.click_visible_elements('input[type="checkbox"]')
671+ """
672+ elements = self .find_elements (selector , by = by )
673+ count = 0
674+ for element in elements :
675+ count += 1
676+ if count == 1 :
677+ self .wait_for_ready_state_complete ()
678+ if self .is_element_visible (selector , by = by ):
679+ self .click (selector , by = by )
680+ else :
681+ self .wait_for_ready_state_complete ()
682+ try :
683+ if element .is_displayed ():
684+ self .__scroll_to_element (element )
685+ element .click ()
686+ except (StaleElementReferenceException , ENI_Exception ):
687+ self .wait_for_ready_state_complete ()
688+ time .sleep (0.05 )
689+ try :
690+ if element .is_displayed ():
691+ self .__scroll_to_element (element )
692+ element .click ()
693+ except (StaleElementReferenceException , ENI_Exception ):
694+ return # Probably on new page / Elements are all stale
695+
666696 def is_element_in_an_iframe (self , selector , by = By .CSS_SELECTOR ):
667697 """ Returns True if the selector's element is located in an iframe.
668698 Otherwise returns False. """
0 commit comments