Send key ends up with invalid Element Exception:InvalidElementStateException: Cannot set the element to '[email protected]'. Did you interact with the correct element?

Hi
I am writing UI tests for android using Appium. I click on a button on my first page in my test method and it navigates to next screen. Below is the screen shot of the navigated page.[Login page]
59%20PM
and the test code to test the Login page is as below.

//To access Login page element

WebElement email =  wait.until(ExpectedConditions.presenceOfElementLocated(MobileBy.id("email_login")));

      email.sendKeys("[email protected]");

      WebElement login_submit =  driver.findElement(MobileBy.id("EmailLoginButton"));
    login_submit.click();
```

When i run the test case written in IntelliJ in emulator by running the Appium server locally, my test case fails with the below message.

org.openqa.selenium.InvalidElementStateException: io.appium.uiautomator2.common.exceptions.InvalidElementStateException: Cannot set the element to ā€˜[email protected]ā€™. Did you interact with the correct element?

As per the test written above, I do use wait.until method to wait for that element with id ā€œemail_loginā€ and then use send keys to set the field but it ends up with above error message as soon as it executes ā€œemail.sendKeys("[email protected]ā€);" code snippet. But I am able to use other commands like email.getText() to read what is in the field. I also verified that the element exists by capturing the page source after the element is found and the page source is as below.

<hierarchy index="0" class="hierarchy" rotation="0" width="1440" height="2712">
  <android.widget.FrameLayout index="0" package="com.sitrion.one" class="android.widget.FrameLayout" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,0][1440,2712]" displayed="true">
    <android.widget.LinearLayout index="0" package="com.sitrion.one" class="android.widget.LinearLayout" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,0][1440,2712]" displayed="true">
      <android.widget.FrameLayout index="0" package="com.sitrion.one" class="android.widget.FrameLayout" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,84][1440,2712]" displayed="true">
        <android.widget.LinearLayout index="0" package="com.sitrion.one" class="android.widget.LinearLayout" text="" resource-id="com.sitrion.one:id/action_bar_root" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,84][1440,2712]" displayed="true">
          <android.widget.FrameLayout index="0" package="com.sitrion.one" class="android.widget.FrameLayout" text="" resource-id="android:id/content" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,84][1440,2712]" displayed="true">
            <android.widget.FrameLayout index="0" package="com.sitrion.one" class="android.widget.FrameLayout" text="" resource-id="com.sitrion.one:id/main_login_container" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,84][1440,2712]" displayed="true">
              <android.widget.ScrollView index="0" package="com.sitrion.one" class="android.widget.ScrollView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,84][1440,2712]" displayed="true">
                <android.widget.LinearLayout index="0" package="com.sitrion.one" class="android.widget.LinearLayout" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,84][1440,1911]" displayed="true">
                  <android.widget.ImageView index="0" package="com.sitrion.one" class="android.widget.ImageView" text="" resource-id="com.sitrion.one:id/logo" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[282,84][1157,1134]" displayed="true" />
                  <android.widget.TextView index="1" package="com.sitrion.one" class="android.widget.TextView" text="Please enter your corporate username." resource-id="com.sitrion.one:id/login_description" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,1152][1440,1214]" displayed="true" />
                  <android.widget.EditText index="2" package="com.sitrion.one" class="android.widget.EditText" text="e.g. [email protected]" resource-id="com.sitrion.one:id/email_login" checkable="false" checked="false" clickable="true" enabled="false" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[0,1232][1440,1400]" displayed="true" />
                  <android.widget.Button index="3" package="com.sitrion.one" class="android.widget.Button" text="Submit" resource-id="com.sitrion.one:id/EmailLoginButton" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[35,1435][1405,1603]" displayed="true" />
                  <android.widget.TextView index="4" package="com.sitrion.one" class="android.widget.TextView" text="By submitting your email address, you agree to the Limeade ONE privacy policy." resource-id="com.sitrion.one:id/txtPrivacyPolicy" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[70,1708][1370,1841]" displayed="true" />
                </android.widget.LinearLayout>
              </android.widget.ScrollView>
              <android.widget.FrameLayout index="1" package="com.sitrion.one" class="android.widget.FrameLayout" text="" resource-id="com.sitrion.one:id/fragmentContainer" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,84][1440,2712]" displayed="true">
                <android.widget.LinearLayout index="0" package="com.sitrion.one" class="android.widget.LinearLayout" text="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,84][1440,2712]" displayed="true">
                  <android.widget.ImageView index="0" package="com.sitrion.one" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,84][1440,609]" displayed="true" />
                  <android.widget.TextView index="1" package="com.sitrion.one" class="android.widget.TextView" text="Get Started" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,609][1440,773]" displayed="true" />
                  <android.widget.TextView index="2" package="com.sitrion.one" class="android.widget.TextView" text="Enter the program code supplied by your employer to continue:" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[35,808][1405,1047]" displayed="true" />
                  <android.widget.LinearLayout index="3" package="com.sitrion.one" class="android.widget.LinearLayout" text="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,1082][1440,1237]" displayed="true">
                    <android.widget.ImageButton index="0" package="com.sitrion.one" class="android.widget.ImageButton" text="" resource-id="com.sitrion.one:id/submit_programcode" checkable="false" checked="false" clickable="true" enabled="false" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[1198,1082][1405,1237]" displayed="true" />
                    <android.widget.EditText index="1" package="com.sitrion.one" class="android.widget.EditText" text="changemaker" resource-id="com.sitrion.one:id/edittext_programcode" checkable="false" checked="false" clickable="true" enabled="false" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[35,1082][1198,1237]" displayed="true" />
                  </android.widget.LinearLayout>
                  <android.widget.TextView index="4" package="com.sitrion.one" class="android.widget.TextView" text="Don't have a code?" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,1237][1440,1364]" displayed="true" />
                </android.widget.LinearLayout>
              </android.widget.FrameLayout>
            </android.widget.FrameLayout>
          </android.widget.FrameLayout>
        </android.widget.LinearLayout>
      </android.widget.FrameLayout>
    </android.widget.LinearLayout>
  </android.widget.FrameLayout>
</hierarchy>

When i run the test case written in IntelliJ in emulator by running the Appium server locally, my test case fails with the below message.

If you check the page source above you will notice that element with email_login does exist but when I set it through test I get the error above.

However, when I start a session through Appium server and use appium page inspector to interact with the views I am able to set the edit field with id ā€œemail_loginā€ and it perfectly accepts it.

I also noticed that if I run the test in debug mode with break points, the field gets set without any issues.

I also noticed that login page does not show up and looks like navigation does not seem to be working from first page to my login page while running the test.

Any pointers of what is wrong here. My page contains fragments and login page is a fragment. Is there an issue in Appium to write tests for fragments?

I am using mac and installed appium desktop from the link https://github.com/appium/appium-desktop/releases/tag/v1.13.0

@rashmiur_ram, Did your issue get resolved? What is the solution of this? I am facing the same issue.

1 Like