clickUsingJS : arguments[0].click is not a function


A lot of threads are talking about clickUsingJS, but my tip doesn’t apply to one in particular, so I put it here :slight_smile:

If the clickUsingJS looks like your only solution, and in case of “org.openqa.selenium.WebDriverException: unknown error: arguments[0].click is not a function”, the following code might works (thanks to a colleague for the help !)

def clickUsingJSAdvanced(TestObject to, int timeout) {
WebElement element = WebUiCommonHelper.findWebElement(to, timeout)
WebUI.executeJavaScript(“arguments[0].dispatchEvent(new MouseEvent(‘click’))”, Arrays.asList(element))

1 Like

Sorry, but…

There’s nothing wrong with .click being called in JavaScript, especially if you make sure it is called as a function using the correct syntax.

arguments[0].click( ) // NOTE THE PARENTHESES!!!

not arguments[0].click

And carrying around all that event object creation logic in your test code when the browser is perfectly capable of doing it for you is not a tip or a trick, it’s just silly.

Again, sorry.

I will edit my initial post to use the correct WebUI function to avoid unnecessary object creation

I also agree that there is nothing wrong with .click (with parentheses of course … it is just the stack trace that does not display it). Fact is, in some cases, you don’t have a .click() function available (in my case, an SVG object).

That’s fair.

But in the case of an SVG element, it’s normally the case that the svg is wrapped in an element that can be clicked, in which case, just click the parent element.

Personally, I’d pass a CSS selector to your click method and let the browser use the regular DOM querySelector interface find the element. IOW, trim all that Driver/WebElement/WebUICommonHelper fat and “go native” :sunglasses: