@@ -1902,13 +1902,24 @@ def click_nth_visible_element(
1902
1902
):
1903
1903
self.__switch_to_newest_window_if_not_blank()
1904
1904
1905
- def click_if_visible(self, selector, by="css selector"):
1905
+ def click_if_visible(self, selector, by="css selector", timeout=0 ):
1906
1906
"""If the page selector exists and is visible, clicks on the element.
1907
1907
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()."""
1909
1911
self.wait_for_ready_state_complete()
1910
1912
if self.is_element_visible(selector, by=by):
1911
1913
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)
1912
1923
1913
1924
def click_active_element(self):
1914
1925
self.wait_for_ready_state_complete()
@@ -5292,19 +5303,41 @@ def js_click(
5292
5303
pass
5293
5304
self.__demo_mode_pause_if_active()
5294
5305
5295
- def js_click_if_present(self, selector, by="css selector"):
5306
+ def js_click_if_present(self, selector, by="css selector", timeout=0 ):
5296
5307
"""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()."""
5298
5311
self.wait_for_ready_state_complete()
5299
5312
if self.is_element_present(selector, by=by):
5300
5313
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)
5301
5323
5302
- def js_click_if_visible(self, selector, by="css selector"):
5324
+ def js_click_if_visible(self, selector, by="css selector", timeout=0 ):
5303
5325
"""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()."""
5305
5329
self.wait_for_ready_state_complete()
5306
5330
if self.is_element_visible(selector, by=by):
5307
5331
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)
5308
5341
5309
5342
def js_click_all(self, selector, by="css selector"):
5310
5343
"""Clicks all matching elements using pure JS. (No jQuery)"""
0 commit comments