Skip to content

Commit 6ac3d02

Browse files
committed
Improve reliability when calling js_click(selector)
1 parent e6a5ec8 commit 6ac3d02

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5016,7 +5016,9 @@ def js_click(
50165016
element = self.wait_for_element_present(
50175017
selector, by=by, timeout=settings.SMALL_TIMEOUT
50185018
)
5019+
scroll_done = False
50195020
if self.is_element_visible(selector, by=by):
5021+
scroll_done = True
50205022
self.__demo_mode_highlight_if_active(selector, by)
50215023
if scroll and not self.demo_mode and not self.slow_mode:
50225024
success = js_utils.scroll_to_element(self.driver, element)
@@ -5050,6 +5052,16 @@ def js_click(
50505052
action = ["js_cl", selector, href_origin, time_stamp]
50515053
if all_matches:
50525054
action[0] = "js_ca"
5055+
if not self.is_element_visible(selector, by=by):
5056+
self.wait_for_ready_state_complete()
5057+
if self.is_element_visible(selector, by=by):
5058+
if scroll and not scroll_done:
5059+
success = js_utils.scroll_to_element(self.driver, element)
5060+
if not success:
5061+
timeout = settings.SMALL_TIMEOUT
5062+
element = page_actions.wait_for_element_present(
5063+
self.driver, selector, by, timeout=timeout
5064+
)
50535065
if not all_matches:
50545066
if ":contains\\(" not in css_selector:
50555067
self.__js_click(selector, by=by)
@@ -11585,7 +11597,16 @@ def __js_click(self, selector, by="css selector"):
1158511597
simulateClick(someLink);"""
1158611598
% css_selector
1158711599
)
11588-
self.execute_script(script)
11600+
try:
11601+
self.execute_script(script)
11602+
except Exception:
11603+
# If the regular mouse-simulated click fails, do a basic JS click
11604+
self.wait_for_ready_state_complete()
11605+
script = (
11606+
"""document.querySelector('%s').click();"""
11607+
% css_selector
11608+
)
11609+
self.execute_script(script)
1158911610

1159011611
def __js_click_all(self, selector, by="css selector"):
1159111612
"""Clicks all matching elements using pure JS. (No jQuery)"""

0 commit comments

Comments
 (0)