1

I'm trying to click some mobile element (Appium) with JavascriptExecutor.
It throws org.openqa.selenium.WebDriverException:
(see below)
My code is very simple.
JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click();", element);
The element above is OK.
So my question is:
Can I use JavascriptExecutorwith Appium mobile elements and if yes - what should I change using the JavascriptExecutor?

unknown error (An unknown server-side error occurred. status='false'. Failed to complete internal method: 'hybridRunJavascript args: [, 0, result = null;resultType = null;resultMessage = null;function getPathToWithSuffix(element,suffix) {if (element.tagName.toUpperCase() == "HTML".toUpperCase())return '//html'+suffix;if (element===document.body)return '//html/body[1]'+suffix;var ix= 0;var siblings= element.parentNode.childNodes;for (var i= 0; i<siblings.length; i++) {var sibling= siblings[i];if (sibling===element)return getPathToWithSuffix(element.parentNode,'/'+element.tagName.toLowerCase()+'['+(ix+1)+']'+suffix);if (sibling.nodeType===1 && sibling.tagName===element.tagName)ix++;}}function runScriptResultWrapper(element){if((typeof element === undefined) || element == null){result = null;resultMessage = null;return;}else if (typeof element === 'string' || element instanceof String){result = element;resultType = "string";resultMessage = null;return;}else if (typeof element === 'number' || element instanceof Number){if(element % 1 === 0){resultType = "int"}else{resultType = "float";}result = element;resultMessage = null;return;}else if (typeof element === 'boolean' || element instanceof Boolean){resultType = "boolean";result = element;resultMessage = null;return;}var paths = [];if(element.constructor === Array || element.length > 0){var elements = element;for (i = 0; i < elements.length; i++) {var elementPath = getPathToWithSuffix(elements[i],"");paths[i] = elementPath;}resultMessage = null;}else if(!element.parentNode){resultType = "object";result = element;resultMessage = null;return;}else{resultMessage = null;paths[0] = getPathToWithSuffix(element,"");}resultType = null;result = {value: paths};};var userFunc = function() {var arguments = new Array();arguments[0]=document.evaluate("(//*[@resource-id='text' or @id='text'])[1]", document, null, XPathResult.ANY_TYPE, null).iterateNext();;arguments[0].click();;};result = null;var temp_result = userFunc();if(result == null) {result = temp_result;} runScriptResultWrapper(result);]', details: Failed to run javascript '': null) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 0 milliseconds

6
  • Your attempted command can be use using appium if you are in web context, this command will not execute on native context Commented Jul 3, 2019 at 12:00
  • So actually JavascriptExecutor not supported by Appium. OK, thanks. Commented Jul 3, 2019 at 12:22
  • Supported, but it's depend on which command do you want to run.. :) Commented Jul 3, 2019 at 13:40
  • For example such basic command as click. But I mean working on a native context, not web Commented Jul 3, 2019 at 16:10
  • Then you can't, because JavaScript execute only on web not native Commented Jul 3, 2019 at 16:16

1 Answer 1

2

In Appium you can use executeScript() method only for running Mobile Commands

like:

Map<String, Object> params = new HashMap<>();
params.put("direction", "down");
params.put("element", ((RemoteWebElement) element).getId());
driver.executeScript("mobile: swipe", params);

So my expectation is that you should use element.click() method instead.

If you have problems with "normal" click - you can consider using SeeTest - Appium Extension Click command which can perform multiple clicks and locate elements using OCR

Sign up to request clarification or add additional context in comments.

5 Comments

I do have problems with the regular element.click() method otherwise I would not try using the JavascriptExecutor and would not ask this question. I will learn the SeeTest above and see if it can solve my problems. Thank you!
Most probably you have wrong locator or something like that, "click()" method should work just fine, do you have any client/server logs? If yes - add them to your question
No, the locator is OK, the problem is that the element appears to not be clickable so clicking on it with regular element.click() works but not works good. It takes a long time and not stable (sometimes timeout occur before the element receives the click)
@DmitriT I was not aware that Javascript was only available for mobile commands, I learned something new. Does this still apply in mobile web apps?
@BillHileman I would accept the answer with pleasure if it solved the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.