Casting exception in io.appium.java

Hi there,
I am having an issue while running the following code,it’s throwing
“Exception in thread “main” java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class io.appium.java_client.MobileElement (java.util.ArrayList is in module java.base of loader ‘bootstrap’; io.appium.java_client.MobileElement is in unnamed module of loader ‘app’)
at appium.AppiumTest.main(AppiumTest.java:42)”

Any help would be appreciated,thanks.

package appium;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebElement;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
public class AppiumTest {

	public static void main(String[] args) {
		
		//Set the Desired Capabilities
		

		File app= new File("C:\\Users\\ikabir\\Desktop\\Andriod-apk\\fbc.mobile.MemberApp.Droid-Signed.apk");
		DesiredCapabilities caps = new DesiredCapabilities();
		//caps.setCapability("udid", "ENUL6303030010"); //Give Device ID of your mobile phone
		caps.setCapability("platformName", "Android");
		caps.setCapability("platformVersion", "7.0");
		caps.setCapability("deviceName", "Galaxy S6");
		caps.setCapability("app", app.getAbsolutePath()); 
		//caps.setCapability("appPackage", "com.android.vending");
		//caps.setCapability("appActivity", "com.google.android.finsky.activities.MainActivity");
		caps.setCapability("appPackage", "Test");
		//caps.setCapability("automationName", "UiAutomator2"); 
	    //caps.setCapability("appActivity", "test.mobile.MemberApp.Droid/test.mobile.MemberApp.Droid.MainActivity");
		caps.setCapability("noReset", "true");
		
		//Instantiate Appium Driver
		try {
	AppiumDriver<MobileElement> driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);

MobileElement emailinput = (MobileElement) driver.findElementsByAccessibilityId(“mailEntry”);
emailinput.click();
emailinput.sendKeys(“[email protected]”);
driver.closeApp();

		} catch (MalformedURLException e) {
			System.out.println(e.getMessage());
		}
	}
 
}

findElementsByAccessibilityId = many elements. Not one. If you need one use

findElementByAccessibilityId

1 Like

@Aleksei thanks for your reply,it worked :).

@Aleksei could you help me resolving the following error:

Exception in thread “main” org.openqa.selenium.WebDriverException: Returned value cannot be converted to WebElement: {element-6066-11e4-a52e-4f735466cecf=8d3561ee-f057-4d5a-9023-c97d4ac5b78e, ELEMENT=8d3561ee-f057-4d5a-9023-c97d4ac5b78e}
Build info: version: ‘3.12.0’, revision: ‘7c6e0b3’, time: ‘2018-05-08T15:15:03.216Z’

Thanks

here is the code that I am trying:
try {
AppiumDriver driver = new AndroidDriver(new URL(“http://0.0.0.0:4723/wd/hub”), caps);
// MobileElement emailinput = (MobileElement) driver.findElementByAccessibilityId(“mailEntry”);
//MobileElement emailinput = driver.findElementByAccessibilityId(“BtnLogin”);
MobileElement emailinput = driver.findElementByXPath("//android.widget.Button[@content-desc=“BtnLogin”]");

		    	 //MobileElement emailinput = (MobileElement) driver.findElementByClassName("android.view.View");
		        emailinput.submit();
		        //emailinput.sendKeys("[email protected]");
			    driver.closeApp();
			
		} catch (MalformedURLException e) {
			System.out.println(e.getMessage());
		}

@Monti_Kabir

  1. what exactly line shows error?
  2. emailinput.submit(); - remove this. use either setValue() or tap element and then sendKeys() (sendKeys requires keyboard open)

@Aleksei this is the line thats causing the error.not sure why it’s showing this “Exception in thread “main” org.openqa.selenium.WebDriverException: Returned value cannot be converted to WebElement: {element-6066-11e4-a52e-4f735466cecf=8d3561ee-f057-4d5a-9023-c97d4ac5b78e, ELEMENT=8d3561ee-f057-4d5a-9023-c97d4ac5b78e}
Build info: version: ‘3.12.0’, revision: ‘7c6e0b3’, time: ‘2018-05-08T15:15:03.216Z’”

I would really appreciate your response,thanks.

@Monti_Kabir try it like:

MobileElement emailinput = (MobileElement) driver.findElementByXPath("//android.widget.Button[@content-desc=“BtnLogin”]");``