Locator Strategy 'css selector' is not supported for this session issue with appium

11,175

Solution 1

Appium is not Selenium: they both implemented using JSON wire protocol and has similar APIs, but not the same ones:

Supported locator strategies for Native android app:

  • id (resource-id View attribute);
  • accessbilityId (content-desc View attribute);
  • uiAutomator (better to read about UiSelector);
  • className (ui component type);
  • XPath.

So you cannot use By.name locator strategy for Android driver session, it's not supported.

To make it easy, I suggest using MobileBy in place of By in Appium native tests: you will get the proper options.

Solution 2

I tried this approach on feb 3rd 2020 and was able to execute succesfully. The change I had to make are update io.appium to 7.2.0.

Pre Requisites in my Case/test: Windows Machine, created Nexus S AVD, Launched Appium and AVD.

Most Importantly

URL url = new URL("http://127.0.0.1:4723/wd/hub");
driver = new AndroidDriver<MobileElement>(url, capabilities);
MobileElement two =(MobileElement)driver.findElement(By.id("com.android.calculator2:id/digit_9"));   
two.click();

Solution 3

By.name("text") is removed from Appium v1.5.0 onwards.

Also just to add cssSelector strategy + methods like getAttribute("color/background") present in Selenium are not supported in Appium for native apps as these are not supported by underlying unit testing framework of Android i.e. UIAutomator.

You can use below options for clicking digits of calculator

driver.findElement(By.id(“com.android.calculator2:id/digit5”)).click();
driver.findElement(By.xpath(“//android.widget.Button[contains(@resource-id,'digit5') and @text='5']”)).click();
driver.findElementByAccessibilityId(“plus”).click();
driver.findElement(By.xpath(“//android.widget.Button[@text='5']”)).click();

Solution 4

Always use Class name and text name to create a xpath.

const loginbutton=await driver.$("//android.widget.TextView[@text='login']")


await loginbutton.click()

Driver comes from capabilities

class name --> android.widget.TextView

text name -->login

so always use class name and text name to create a better and nicer xpath.

works for all android+appium+wdio

Share:
11,175

Related videos on Youtube

h3r
Author by

h3r

Updated on June 04, 2022

Comments

  • h3r
    h3r almost 2 years

    Since I'm new to mobile automation, I've been trying to run simple activities using appium maven and eclipse. But When I try to run Calculator app opens but the elements are not accessible.

    This the code I used to run a simple calculator

      @BeforeClass
     public void setUp() throws MalformedURLException {
    
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("BROWSER_NAME", "Android");
        capabilities.setCapability("VERSION", "4.4.2");
        capabilities.setCapability("deviceName", "Emulator");
        capabilities.setCapability("platformName", "Android");
    
        capabilities.setCapability("appPackage", "com.android.calculator2");
    
        capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
    
        driver = new RemoteWebDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
    }
    
    @Test
    public void testCal() throws Exception {
    
        WebElement two = driver.findElement(By.name("2"));
        two.click();
    
    }
    
    @AfterClass
    public void teardown() {
    
    }
    

    I am using the latest stable dependencies. io.appium java-client 7.0.0 and org.testng testng 6.14.3

    FAILED: testCal
    org.openqa.selenium.InvalidSelectorException: Locator Strategy 'css 
    selector' is not supported for this session
    For documentation on this error, please visit: 
    https://www.seleniumhq.org/exceptions/invalid_selector_exception.html
    Driver info: org.openqa.selenium.remote.RemoteWebDriver
    Capabilities {BROWSER_NAME: Android, VERSION: 4.4.2, appActivity: 
    com.android.calculator2.Cal..., appPackage: com.android.calculator2, 
    databaseEnabled: false, desired: {BROWSER_NAME: Android, VERSION: 4.4.2, 
    appActivity: com.android.calculator2.Cal..., appPackage: 
    com.android.calculator2, deviceName: Emulator, platformName: android}, 
    deviceManufacturer: HUAWEI, deviceModel: FLA-LX2, deviceName: 
    HXT7N18521000819, deviceScreenSize: 1080x2160, deviceUDID: 
    HXT7N18521000819, javascriptEnabled: true, locationContextEnabled: false, 
    networkConnectionEnabled: true, platform: LINUX, platformName: LINUX, 
    platformVersion: 8.0.0, takesScreenshot: true, warnings: {}, 
    webStorageEnabled: false}
    Session ID: a604a166-3c0d-4e9c-a3e4-9b1ea734bee6
    *** Element info: {Using=name, value=2}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown 
    Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at...
    
  • Vasanth
    Vasanth about 4 years
    I'm trying to test the Hybrid Mobile application written with Ionic. Do you have any suggestions on that ? I have tried switching the context to web, but I'm stuck with that too.
  • Ananth Kamath
    Ananth Kamath over 2 years
    If you removed selenium-java dependencies, then how did you use DesiredCapabilities? Could you share your code and also the pom.xml file please?