Skip to content

Commit 953340a

Browse files
committed
Add timeout options to the click_if_visible() methods
1 parent e8eb0c6 commit 953340a

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

help_docs/method_summary.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ self.click_visible_elements(selector, by="css selector", limit=0, timeout=None)
126126

127127
self.click_nth_visible_element(selector, number, by="css selector", timeout=None)
128128

129-
self.click_if_visible(selector, by="css selector")
129+
self.click_if_visible(selector, by="css selector", timeout=0)
130130

131131
self.click_active_element()
132132

@@ -356,9 +356,9 @@ self.click_xpath(xpath)
356356

357357
self.js_click(selector, by="css selector", all_matches=False, scroll=True)
358358

359-
self.js_click_if_present(selector, by="css selector")
359+
self.js_click_if_present(selector, by="css selector", timeout=0)
360360

361-
self.js_click_if_visible(selector, by="css selector")
361+
self.js_click_if_visible(selector, by="css selector", timeout=0)
362362

363363
self.js_click_all(selector, by="css selector")
364364

seleniumbase/fixtures/base_case.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,13 +1902,24 @@ def click_nth_visible_element(
19021902
):
19031903
self.__switch_to_newest_window_if_not_blank()
19041904

1905-
def click_if_visible(self, selector, by="css selector"):
1905+
def click_if_visible(self, selector, by="css selector", timeout=0):
19061906
"""If the page selector exists and is visible, clicks on the element.
19071907
This method only clicks on the first matching element found.
1908-
(Use click_visible_elements() to click all matching elements.)"""
1908+
Use click_visible_elements() to click all matching elements.
1909+
If a "timeout" is provided, waits that long for the element
1910+
to appear before giving up and returning without a click()."""
19091911
self.wait_for_ready_state_complete()
19101912
if self.is_element_visible(selector, by=by):
19111913
self.click(selector, by=by)
1914+
elif timeout > 0:
1915+
try:
1916+
self.wait_for_element_visible(
1917+
selector, by=by, timeout=timeout
1918+
)
1919+
except Exception:
1920+
pass
1921+
if self.is_element_visible(selector, by=by):
1922+
self.click(selector, by=by)
19121923

19131924
def click_active_element(self):
19141925
self.wait_for_ready_state_complete()
@@ -5292,19 +5303,41 @@ def js_click(
52925303
pass
52935304
self.__demo_mode_pause_if_active()
52945305

5295-
def js_click_if_present(self, selector, by="css selector"):
5306+
def js_click_if_present(self, selector, by="css selector", timeout=0):
52965307
"""If the page selector exists, js_click() the element.
5297-
This method only clicks on the first matching element found."""
5308+
This method only clicks on the first matching element found.
5309+
If a "timeout" is provided, waits that long for the element to
5310+
be present before giving up and returning without a js_click()."""
52985311
self.wait_for_ready_state_complete()
52995312
if self.is_element_present(selector, by=by):
53005313
self.js_click(selector, by=by)
5314+
elif timeout > 0:
5315+
try:
5316+
self.wait_for_element_present(
5317+
selector, by=by, timeout=timeout
5318+
)
5319+
except Exception:
5320+
pass
5321+
if self.is_element_present(selector, by=by):
5322+
self.js_click(selector, by=by)
53015323

5302-
def js_click_if_visible(self, selector, by="css selector"):
5324+
def js_click_if_visible(self, selector, by="css selector", timeout=0):
53035325
"""If the page selector exists and is visible, js_click() the element.
5304-
This method only clicks on the first matching element found."""
5326+
This method only clicks on the first matching element found.
5327+
If a "timeout" is provided, waits that long for the element
5328+
to appear before giving up and returning without a js_click()."""
53055329
self.wait_for_ready_state_complete()
53065330
if self.is_element_visible(selector, by=by):
53075331
self.js_click(selector, by=by)
5332+
elif timeout > 0:
5333+
try:
5334+
self.wait_for_element_visible(
5335+
selector, by=by, timeout=timeout
5336+
)
5337+
except Exception:
5338+
pass
5339+
if self.is_element_visible(selector, by=by):
5340+
self.js_click(selector, by=by)
53085341

53095342
def js_click_all(self, selector, by="css selector"):
53105343
"""Clicks all matching elements using pure JS. (No jQuery)"""

0 commit comments

Comments
 (0)