Search code examples
javascriptappiumappium-android

How to get element by resource id?


I have spent some hours trying to search an element by id using appium javascript client with no luck. Have found some other posts here in stack overflow stating it works, but it doesn not work for me. It seems that I could use something like:

var buttonEl = await driver.findElement(By.id("resourceid"));

but I always get an error saying:

InvalidSelectorError: Locator Strategy 'css selector' is not supported for this session

Here the source code:

"use strict";
 
var wd = require("selenium-webdriver"),
    By = wd.By,
    until = wd.until;

// Setting Desired Capabilities.
var desiredCaps = {
    platformName: "Android",
    deviceName: "a3ae1c63",
    appPackage: "com.mypackage",
    appActivity: ".Main",
    browserName: '',
    noReset: true,
    newCommandTimeout: 1000000

};

async function test() {
        //Initiating the Driver
        let driver = await new wd.Builder().usingServer("http://localhost:4723/wd/hub").withCapabilities(desiredCaps).build();
         
        var buttonEl = await driver.findElement(By.id("id/contact_selector"));
            buttonEl.click();

        }

test();

I'm pretty confused now. I understand that it seems that I can't use the find element by id in an android session but on the other hand I have read about people using it successfully.

Any help would be greatly appreciated.


Solution

  • Finally I was able to fix my problem. The main problem was that I wasn't using the needed web driver. I switched to webdriverio and everything is working now.

    Better place to start is to check examples in appium github. They have some examples for every driver. Code is a bit outdated though.