Message: Cannot set the element to '[email protected]'. Did you interact with the correct element?

I am doing automation of the app using appium. I am able to launch the app but I go for login, it can read the XPath for login. But appium is unable to enter the mobile number. For mobile device, I am using the emulator. I’ve attached the logs for your reference. logs.txt (1.9 KB)

This is my code:

email = driver.find_element(By.XPATH, '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout'
                                              '/android.widget.FrameLayout/android.widget.FrameLayout/android.widget'
                                              '.FrameLayout/android.view.View/android.view.View/android.view.View'
                                              '/android.view.View/android.view.View['
                                              '2]/android.view.View/android.widget.EditText[1]')
        email.click()
        time.sleep(5)
        email.send_keys('[email protected]')

Error facing:

Encountered internal error running command:
selenium.common.exceptions.InvalidElementStateException: Message: Cannot set the element to ‘[email protected]’. Did you interact with the correct element?

  1. dont use such long xpath
  2. try never use xpath
  3. after you tap on input whole path to element may change!. See 1 + 2
  4. instead of xpath use ids. if your app do not have ids -> ask developers to set it.
  5. if your android developers cant do this by any reason (or maybe you just cant contact to them) try use classname:
        List<MobileElement> inputs = driver.findElements(MobileBy.className("android.widget.EditText")); // now we have all inputs
        // fastest way (may not 100% work with all apps. depends on your app)
        inputs.get(0).setValue("[email protected]"); // ' inputs.get(0)' = take first input
        // or
        inputs.get(0).sendKeys("[email protected]");
        // if fastest way does not work and app need to open keyboard first
        inputs.get(0).click(); // TODO replace click -> tap. Tap = mobile, Click = web. Click is not nice way for mobile.
        // sometimes you need find element again after tap. add it here if it happens.
        inputs.get(0).setValue("[email protected]");
        // or
        inputs.get(0).sendKeys("[email protected]");

Hi Aleksei,

Changed the code to
email = driver.find_element(By.CLASS_NAME, ‘android.widget.EditText’)
email.click()
time.sleep(5)
email.send_keys(‘[email protected]’)

This is all I have other than xpath -
|elementId|4ca8b2ad-8930-44ac-b8a1-72efba6c35a1|
|index|1|
|package|com.ghc.marsapp|
|class|android.widget.EditText|
|text|Email|
|resource-id||
|checkable|false|
|checked|false|
|clickable|true|
|enabled|true|
|focusable|true|
|focused|false|
|long-clickable|false|
|password|false|
|scrollable|false|
|selected|false|
|bounds|[122,1046][1318,1252]|
|displayed|true|

Also, tried searching by element id but still getting the same error.
selenium.common.exceptions.InvalidElementStateException: Message: Cannot set the element to ‘[email protected]’. Did you interact with the correct element?

email = driver.find_element(By.CLASS_NAME, ‘android.widget.EditText’)
email.click()
time.sleep(5)
email = driver.find_element(By.CLASS_NAME, ‘android.widget.EditText’) // try add to search element again
email.send_keys(‘[email protected]’)

also try instead of ‘send_keys’:

It’s still the same. Tried with XPATH as well.

Same cant happen when using Actions way. Should be other error. Show it.

C:\Users\purbita\PycharmProjects\GHC\venv\Scripts\python.exe “C:\Program Files\JetBrains\PyCharm Community Edition 2020.3.5\plugins\python-ce\helpers\pycharm_jb_unittest_runner.py” --target Main.login_signup.test_login
Testing started at 00:14 …
Launching unittests with arguments python -m unittest Main.login_signup.test_login in C:\Users\purbita\PycharmProjects\GHC\Login

Ran 1 test in 63.902s

FAILED (errors=1)

Error
Traceback (most recent call last):
File “C:\Users\purbita\AppData\Local\Programs\Python\Python39\lib\unittest\case.py”, line 59, in testPartExecutor
yield
File “C:\Users\purbita\AppData\Local\Programs\Python\Python39\lib\unittest\case.py”, line 593, in run
self._callTestMethod(testMethod)
File “C:\Users\purbita\AppData\Local\Programs\Python\Python39\lib\unittest\case.py”, line 550, in _callTestMethod
method()
File “C:\Users\purbita\PycharmProjects\GHC\Login\Main.py”, line 29, in test_login
email.send_keys("[email protected]")
File “C:\Users\purbita\PycharmProjects\GHC\venv\lib\site-packages\appium\webdriver\webelement.py”, line 218, in send_keys
self._execute(RemoteCommand.SEND_KEYS_TO_ELEMENT, {‘text’: ‘’.join(keys), ‘value’: keys})
File “C:\Users\purbita\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py”, line 633, in _execute
return self._parent.execute(command, params)
File “C:\Users\purbita\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py”, line 321, in execute
self.error_handler.check_response(response)
File “C:\Users\purbita\PycharmProjects\GHC\venv\lib\site-packages\appium\webdriver\errorhandler.py”, line 31, in check_response
raise wde
File “C:\Users\purbita\PycharmProjects\GHC\venv\lib\site-packages\appium\webdriver\errorhandler.py”, line 26, in check_response
super().check_response(response)
File “C:\Users\purbita\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py”, line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidElementStateException: Message: Cannot set the element to ‘[email protected]’. Did you interact with the correct element?

a was asking to show error with such code.