Search code examples
javaandroidandroid-ndkappiumnative

Using findElementById("elementID") with appium for native android app. Error: "Locator strategy 'css selector" is not supported"


I am using Appium installed over npm (version 1.13.0), Selenium (3.8.1), Appium client for java (4.1.2).

Using UIAutomationViewer I am able to find IDs of elements I want to click. But when I run the code, in place od IDs, selenium is trying to find them with css.

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, 
"PixelTest");
capabilities.setCapability("platformName","Android");
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, 
"UiAutomator1");
capabilities.setCapability(MobileCapabilityType.NO_RESET, true);
capabilities.setCapability("appPackage", "com.android.calculator2");
capabilities.setCapability("appActivity", ".Calculator");

driver = new AndroidDriver<AndroidElement>(new URL(url), capabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.findElementById("digit_9").click();
driver.findElementById("digit_6").click();

driver.findElementById("pad_operator").click();

driver.findElementById("digit_1").click();
driver.findElementById("digit_0").click();  

driver.findElementById("eq").click();

Error I am having: "org.openqa.selenium.InvalidSelectorException: Locator Strategy 'css selector' is not supported for this session". Why this happens?


Solution

  • You're using incompatible Selenium Java and Appium Java clients combination.

    Looking into dependencies for Appium 4.1.2 it appears that you need Selenium 2.53.1, other versions might not work due to JAR Hell.

    enter image description here

    Basically you need to have only Appium Java in your project classpath, if you're using a build system like Maven or Gradle - the other jars will be resolved via Transitive Dependencies mechanism.

    I would also recommend upgrading to Appium Client 7.0.0, or whatever is the latest version released.

    Check out Appium - Code Examples - Java for sample projects you can use as a basis.