Skip to content

Commit 7776b79

Browse files
committed
Improve the reliability of js_click(selector)
1 parent cb3f454 commit 7776b79

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5016,6 +5016,8 @@ def js_click(
50165016
element = self.wait_for_element_present(
50175017
selector, by=by, timeout=settings.SMALL_TIMEOUT
50185018
)
5019+
if not page_actions.is_element_clickable(self.driver, selector, by):
5020+
self.wait_for_ready_state_complete()
50195021
scroll_done = False
50205022
if self.is_element_visible(selector, by=by):
50215023
scroll_done = True
@@ -11584,6 +11586,8 @@ def __js_click(self, selector, by="css selector"):
1158411586
css_selector = self.convert_to_css_selector(selector, by=by)
1158511587
css_selector = re.escape(css_selector) # Add "\\" to special chars
1158611588
css_selector = self.__escape_quotes_if_needed(css_selector)
11589+
is_visible = self.is_element_visible(selector, by=by)
11590+
current_url = self.get_current_url()
1158711591
script = (
1158811592
"""var simulateClick = function (elem) {
1158911593
var evt = new MouseEvent('click', {
@@ -11600,10 +11604,18 @@ def __js_click(self, selector, by="css selector"):
1160011604
try:
1160111605
self.execute_script(script)
1160211606
except Exception as e:
11607+
# If element was visible but no longer, or on a different page now,
11608+
# assume that the click actually worked and continue with the test.
11609+
if (
11610+
(is_visible and not self.is_element_visible(selector, by=by))
11611+
or current_url != self.get_current_url()
11612+
):
11613+
return # The click worked, but threw an Exception. Keep going.
11614+
# It appears the first click didn't work. Make another attempt.
1160311615
self.wait_for_ready_state_complete()
1160411616
if "Cannot read properties of null" in e.msg:
1160511617
page_actions.wait_for_element_present(
11606-
self.driver, selector, by, timeout=3
11618+
self.driver, selector, by, timeout=5
1160711619
)
1160811620
if not page_actions.is_element_clickable(
1160911621
self.driver, selector, by

0 commit comments

Comments
 (0)