diff --git a/selene/core/command.py b/selene/core/command.py index 2d683b45..a2275f42 100644 --- a/selene/core/command.py +++ b/selene/core/command.py @@ -232,7 +232,37 @@ def func(element: Element): click: Command[Element] = Command( 'click', # TODO: should we process collections too? i.e. click through all elements? - lambda element: element.execute_script('element.click()'), + lambda element: element.execute_script( + ''' + const offsetX = arguments[0] + const offsetY = arguments[1] + const rect = element.getBoundingClientRect() + + function mouseEvent() { + if (typeof (Event) === 'function') { + return new MouseEvent('click', { + view: window, + bubbles: true, + cancelable: true, + clientX: rect.left + rect.width / 2 + offsetX, + clientY: rect.top + rect.height / 2 + offsetY + }) + } + else { + const event = document.createEvent('MouseEvent') + event.initEvent('click', true, true) + event.type = 'click' + event.view = window + event.clientX = rect.left + rect.width / 2 + offsetX + event.clientY = rect.top + rect.height / 2 + offsetY + return event + } + } + element.dispatchEvent(mouseEvent()) + ''', + 0, + 0, + ), ) clear_local_storage: Command[Browser] = Command( diff --git a/tests/integration/element__perform__js__click_test.py b/tests/integration/element__perform__js__click_test.py new file mode 100644 index 00000000..cbe936d3 --- /dev/null +++ b/tests/integration/element__perform__js__click_test.py @@ -0,0 +1,46 @@ +import time + +from selene import command +from tests.integration.helpers.givenpage import GivenPage + + +def test_command_js_click_does_not_wait_for_no_overlay(session_browser): + browser = session_browser.with_(timeout=0.5) + page = GivenPage(browser.driver) + page.opened_with_body( + ''' +