Error being returned for long_press in python client : {"required":["elements"]} and you sent ["sessionId","element"]

I’m having trouble using long_press in the python client. I hope I’m doing something silly as I’m new to appium. Any help would be appreciated figuring this out.

The error being returned is {“required”:[“elements”]} and you sent [“sessionId”,“element”] for long_press.

Version’s, Caps and imports
appium 1.6.3

from appium import webdriver
from selenium.webdriver.common.touch_actions import TouchActions

desired_caps = {}
desired_caps[‘platformName’] = ‘iOS’
desired_caps[‘platformVersion’] = platformVersion
desired_caps[‘deviceName’] = deviceName
desired_caps[‘udid’] = udid
desired_caps[‘automationName’] = ‘XCUITest’
desired_caps[‘realDeviceLogger’] = ‘idevicesyslog’
desired_caps[‘app’] = os.path.abspath(app_path)
desired_caps[‘xcodeConfigFile’] = ‘/usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent/xcodeConfigfile.xcconfig’
desired_caps[‘xcodeSigningId’] = ‘iPhone Developer’
desired_caps[‘newCommandTimeout’] = 10000

Code below is inside a function:
print(“Inside long_press_on_model”)
model = self.driver.find_element_by_id(model_name)

current = self.driver.current_context
print("This is the current driver " + current)

# print("This is the dir of the webElement " + str(dir(model)))
print("this is the model" + str(model)) 
action = Base.TouchActions(self.driver)
action.long_press(model).perform()

Terminal output:
Inside long_press_on_model
This is the current driver NATIVE_APP
this is the model appium.webdriver.webelement.WebElement (session=“e9c97185-1f75-4870-9e73-9818b96d4e82”, element=“2837406B-2D88-4D82-94E0-49C5EE53FF44”)
Traceback (most recent call last):
File “tests/test.py”, line 44, in
myFunc.long_press_on_model(model)
File “page_models/my_test.py”, line 66, in long_press_on_model
action.long_press(model).perform()
File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/common/touch_actions.py”, line 47, in perform
action()
File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/common/touch_actions.py”, line 151, in
Command.LONG_PRESS, {‘element’: on_element.id}))
File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 236, in execute
self.error_handler.check_response(response)
File “/usr/local/lib/python2.7/site-packages/appium/webdriver/errorhandler.py”, line 29, in check_response
raise wde
selenium.common.exceptions.WebDriverException: Message: Parameters were incorrect. We wanted {“required”:[“elements”]} and you sent [“sessionId”,“element”]

Appium log:
[debug] [MJSONWP] Responding to client with driver.findElement() result: {“ELEMENT”:“2837406B-2D88-4D82-94E0-49C5EE53FF44”,“type”:“XCUIElementTypeStaticText”,“label”:“model”}
[HTTP] <-- POST /wd/hub/session/e9c97185-1f75-4870-9e73-9818b96d4e82/element 200 768 ms - 199
[HTTP] --> GET /wd/hub/session/e9c97185-1f75-4870-9e73-9818b96d4e82/context {}
[debug] [MJSONWP] Calling AppiumDriver.getCurrentContext() with args: [“e9c97185-1f75-4870-9e73-9818b96d4e82”]
[debug] [XCUITest] Executing command ‘getCurrentContext’
[debug] [MJSONWP] Responding to client with driver.getCurrentContext() result: “NATIVE_APP”
[HTTP] <-- GET /wd/hub/session/e9c97185-1f75-4870-9e73-9818b96d4e82/context 200 3 ms - 84
[HTTP] --> POST /wd/hub/session/e9c97185-1f75-4870-9e73-9818b96d4e82/touch/longclick {“sessionId”:“e9c97185-1f75-4870-9e73-9818b96d4e82”,“element”:“2837406B-2D88-4D82-94E0-49C5EE53FF44”}
[debug] [MJSONWP] Bad parameters: BadParametersError: Parameters were incorrect. We wanted {“required”:[“elements”]} and you sent [“sessionId”,“element”]
[HTTP] <-- POST /wd/hub/session/e9c97185-1f75-4870-9e73-9818b96d4e82/touch/longclick 400 2 ms - 99

Looks like you are passing an empty string to a method that requires a string.

Hi Wreed,

Sorry that was a copy and paste error. Actually this website removed it, guessing it thought is was code/script tags.

Please see Appium logs: [debug] [MJSONWP] Bad parameters: BadParametersError: Parameters were incorrect. We wanted {“required”:[“elements”]} and you sent [“sessionId”,“element”].

I passed in [“sessionId”,“element”] and it wanted {“required”:[“elements”]}. Here is another run :

Inside long_press_on_model
This is the current driver NATIVE_APP
this is the model appium.webdriver.webelement.WebElement (session=“55613931-19a2-4234-b2fe-79037d73aea2”, element=“A35CE8ED-8BB6-458D-983C-EC7412E8A2AE”)
Traceback (most recent call last):
File “tests/test.py”, line 44, in
myTest.long_press_on_model(model)
File “page_models/my_test.py”, line 65, in long_press_on_model
action.long_press(model).perform()
File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/common/touch_actions.py”, line 47, in perform
action()
File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/common/touch_actions.py”, line 151, in
Command.LONG_PRESS, {‘element’: on_element.id}))
File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 236, in execute
self.error_handler.check_response(response)
File “/usr/local/lib/python2.7/site-packages/appium/webdriver/errorhandler.py”, line 29, in check_response
raise wde
selenium.common.exceptions.WebDriverException: Message: Parameters were incorrect. We wanted {“required”:[“elements”]} and you sent [“sessionId”,“element”]

I looked at this again and am changing my reply.

You are getting a return value of this:

And it looks like you need to parse that reply and only return this:

"2837406B-2D88-4D82-94E0-49C5EE53FF44"

And that is what is needed as a parameter to long_press.

When I pass it the string of the id I get the following error: AttributeError: ‘unicode’ object has no attribute ‘id’

See below:
model = self.driver.find_element_by_id(model_name)
tmp_arr = [str(model.id)]

File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/common/touch_actions.py”, line 47, in perform
action()
File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/common/touch_actions.py”, line 151, in
Command.LONG_PRESS, {‘element’: on_element.id}))
AttributeError: ‘unicode’ object has no attribute ‘id’

From the api doc its seems long_press is meant to operate on “on_element”. I would have though this is a webelement.
https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.touch_actions.html#module-selenium.webdriver.common.touch_actions

So when I find the element by the below its returning a webelement.
model = self.driver.find_element_by_id(model_name)

I cant seem to be able to access the element part of the object to pass through. How do I access the element part as an object to pass through?

Oh, webelements. I’m actually not sure about those. It’s weird though because your context is clearly ‘NATIVE_APP’. Could it be that you need to switch to ‘WEBVIEW’ to work with this element?

Hmmm, this is a native app. I will try and see how that works.

So I iterated over driver.contexts to see if WEBVIEW was available and it was not. As this is an ios native APP that seems reasonable.

Any other suggestions?

What does this return you:

model = self.driver.find_element_by_name(model_name)

The element is not named so that’s why I’m using the id. Could I ask why you would like me to use “model = self.driver.find_element_by_name(model_name)” as this also returns a webelement?

Am I missing something here?

https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.touch_actions.html#module-selenium.webdriver.common.touch_actions

Sure, I looked at what is returned in the logs:

[debug] [MJSONWP] Responding to client with driver.findElement() result: {"ELEMENT":"2837406B-2D88-4D82-94E0-49C5EE53FF44","type":"XCUIElementTypeStaticText","label":"model"}

We can see that it has a label, called ‘model’. Most of the time when I’m working with an element like that the ‘label’ and ‘name’ are interchangeable. You are saying that it does return a webelement so it sounds like it worked but did not return the correct object you’d like to use as a parameter for the long press. Sometimes troubleshooting is taking a chance or 2. Sorry it didn’t work.

Thanks for you help. I appreciate the time and effort you have contributed to this issue.