@@ -640,53 +640,67 @@ def is_text_visible(self, text, selector="html", by=By.CSS_SELECTOR):
640
640
by = By .LINK_TEXT
641
641
return page_actions .is_text_visible (self .driver , text , selector , by )
642
642
643
- def find_elements (self , selector , by = By .CSS_SELECTOR ):
644
- """ Returns a list of matching WebElements. """
643
+ def find_elements (self , selector , by = By .CSS_SELECTOR , limit = 0 ):
644
+ """ Returns a list of matching WebElements.
645
+ If "limit" is set and > 0, will only return that many elements. """
645
646
self .wait_for_ready_state_complete ()
646
647
if page_utils .is_xpath_selector (selector ):
647
648
by = By .XPATH
648
649
if page_utils .is_link_text_selector (selector ):
649
650
selector = page_utils .get_link_text_from_selector (selector )
650
651
by = By .LINK_TEXT
651
- return self .driver .find_elements (by = by , value = selector )
652
-
653
- def find_visible_elements (self , selector , by = By .CSS_SELECTOR ):
654
- """ Returns a list of matching WebElements that are visible. """
652
+ elements = self .driver .find_elements (by = by , value = selector )
653
+ if limit and limit > 0 and len (elements ) > limit :
654
+ elements = elements [:limit ]
655
+ return elements
656
+
657
+ def find_visible_elements (self , selector , by = By .CSS_SELECTOR , limit = 0 ):
658
+ """ Returns a list of matching WebElements that are visible.
659
+ If "limit" is set and > 0, will only return that many elements. """
655
660
self .wait_for_ready_state_complete ()
656
661
if page_utils .is_xpath_selector (selector ):
657
662
by = By .XPATH
658
663
if page_utils .is_link_text_selector (selector ):
659
664
selector = page_utils .get_link_text_from_selector (selector )
660
665
by = By .LINK_TEXT
661
- return page_actions .find_visible_elements (self .driver , selector , by )
666
+ v_elems = page_actions .find_visible_elements (self .driver , selector , by )
667
+ if limit and limit > 0 and len (v_elems ) > limit :
668
+ v_elems = v_elems [:limit ]
669
+ return v_elems
662
670
663
- def click_visible_elements (self , selector , by = By .CSS_SELECTOR ):
671
+ def click_visible_elements (self , selector , by = By .CSS_SELECTOR , limit = 0 ):
664
672
""" Finds all matching page elements and clicks visible ones in order.
665
673
If a click reloads or opens a new page, the clicking will stop.
666
674
Works best for actions such as clicking all checkboxes on a page.
667
675
Example: self.click_visible_elements('input[type="checkbox"]')
668
- """
676
+ If "limit" is set and > 0, will only click that many elements. """
669
677
elements = self .find_elements (selector , by = by )
670
678
count = 0
679
+ click_count = 0
671
680
for element in elements :
681
+ if limit and limit > 0 and click_count >= limit :
682
+ return
672
683
count += 1
673
684
if count == 1 :
674
685
self .wait_for_ready_state_complete ()
675
686
if self .is_element_visible (selector , by = by ):
676
687
self .click (selector , by = by )
688
+ click_count += 1
677
689
else :
678
690
self .wait_for_ready_state_complete ()
679
691
try :
680
692
if element .is_displayed ():
681
693
self .__scroll_to_element (element )
682
694
element .click ()
695
+ click_count += 1
683
696
except (StaleElementReferenceException , ENI_Exception ):
684
697
self .wait_for_ready_state_complete ()
685
698
time .sleep (0.05 )
686
699
try :
687
700
if element .is_displayed ():
688
701
self .__scroll_to_element (element )
689
702
element .click ()
703
+ click_count += 1
690
704
except (StaleElementReferenceException , ENI_Exception ):
691
705
return # Probably on new page / Elements are all stale
692
706
0 commit comments