Skip to content

Commit 8016dba

Browse files
committed
Add click-nth-visible-element(selector, number)
1 parent 51f2d56 commit 8016dba

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,28 @@ def click_visible_elements(self, selector, by=By.CSS_SELECTOR, limit=0):
10011001
except (StaleElementReferenceException, ENI_Exception):
10021002
return # Probably on new page / Elements are all stale
10031003

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+
10041026
def click_if_visible(self, selector, by=By.CSS_SELECTOR):
10051027
""" If the page selector exists and is visible, clicks on the element.
10061028
This method only clicks on the first matching element found.

0 commit comments

Comments
 (0)