MobileElement Class cast exception

After straggling on 0.18.2 for way too long, we’re finally making the plunge to 1.0.

I’m trying to use setValue as a MobileElement object, but I’m getting a class cast exception, anyone know what might cause this?

Selenium version: 2.42.2
Appium Driver: 2.0.0

MobileElement e = (MobileElement) client.findElement(By.xpath("//UIATextField[@name=‘Email’]"));

java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebElement cannot be cast to io.appium.java_client.MobileElement

This is problem is interesting for me!

Please look at this code sample

WebDriver d = getWrappedDriver();
MobileElement me = (MobileElement) d.findElement(“”));
MobileElement me2 = (MobileElement) me.findElement(By.className(“android.widget.RelativeLayout”));

It work fine.

As you can see I instantiate AndroidDriver which generates AndroidElement instances. IOSDriver is similar and it generates IOSElement’s. Both AndroidElement and IOSElement extend MobileElement.

Information is not enough to fing the root cause. Please share your code sample. Do you use RemoteWebDriver instead of IOSDriver? It seems you do that.

You are partially correct, I am instantiating AppiumDriver, not IOSDriver. Do I have to use IOSDriver to use setValue? We share test code between android and ios so instantiating a specific driver class would break the tests on the other platform.

Plese read my answer here: iOS/Android Driver casting on a shared test code base

How do you instantiate AppiumDriver? It is abstract since 2.0.0.

@SergeyTichomirov: I’m also facing the same issue:
org.openqa.selenium.remote.RemoteWebElement cannot be cast to io.appium.java_client.MobileElement

Though I’m using AppiumDriver, had instantiated with
AppiumDriver appiumDriver=(AppiumDriver) webdriver; and while inspecting I can find it instantiated, also i’m able to use other methods like resetApp();

using IOS 5s simulator, xcode 6.1, appium 1.3.1, java, webdriver

Hi @p00j4!

I highlighted the root cause

What is your Appium java-client version? It seems that you use very old version (1.5.0 or lower). With old versions we have to do this

MobileElement e = new MobileElement(originalElement);

Since 1.6.0 it was fixed (as I know).
If you use 2.0.0 it is weird that you can instantiate AppiumDriver because it is abstract and there must be compilation problems.

AndroidDriver and IOSDriver are supposed to be used and they extend abstract AppiumDriver. So there shouldn’t be cast problems.

1 Like

yes @SergeyTichomirov : you are r8, thanks 4 pointing into r8 direction. i’m updating the client to 2.0.0
but in this case I need to create 2 objects for AndroidDriver & IOSDriver in the same test framework in which both test scripts lies (Android, ios) , so just wanted to know if it works well [related methods] with old/latest ios simulators !

All right!
About the solving of a problem with two AppiumDrivers I wrote here: iOS/Android Driver casting on a shared test code base

Please read this :slight_smile:

1 Like

yes I had read your solution @SergeyTichomirov, I’m curious to know if it works well with latest simulators IOS 8+xcode 6 etc.

So let’s try it :smile: