Selecting correct XPath

In my app I have -

Based on the number of courts the frameLayout Count will vary.
Now I want to press on the + button inside that frameLayout of a specific court which i will randomly select based on the frameLayout index

So far I have tried using:

exports.selectSlotsForTicketing = async function (driver, ticketCount) {
    let courts = await driver.elementsById('com.techmash.playo:id/card_view');
    if (courts.length > 0) {
        //taking random index between 0 and total courts available

        let courtIndex = Math.floor((Math.random() * courts.length));
        console.log('Selecting court :'.green, courtIndex)
        for (let i = 0; i < ticketCount; i++) {
            let ticketXPath = "//android.widget.FrameLayout[contains(@index,'" + courtIndex + "')]/android.widget.ImageButton[contains(@resource-id,'com.techmash.playo:id/extra_plus_button')]";
            await driver.elementByXPath(ticketXPath).click();
        }
    }
};

But seems like the xpath for the +button is not correct.
Can anyone guide me on selecting the correct xpath here.
Basically I want to know the correct way to write xpath of child of childs of a class

@shashikumarraja
I thought about some different approaches:
The first one would change the logic of your code, instead of look for each card, you try to use "//android.widget.FrameLayout[contains(@resource-id,'cardview')]//android.widget.ImageButton[contains(@resource-id,'extra_plus_button')]" with elementsByXPath. This should give you a list of all the extra_plus_button, so you can choose any of them.

Another option you can try to look for the extra_plus_button form the courts[index] element, and look for the Xpath like “//android.widget.ImageButton[contains(@resource-id,‘extra_plus_button’)]”

From your current I think it’s missing one / before the ImageButtons so it would be like:
//android.widget.FrameLayout[contains(@index,’” + courtIndex + “’)]**//**android.widget.ImageButton[contains(@resource-id,‘com.techmash.playo:id/extra_plus_button’)]”

Have a look at w3 school xpath.

1 Like

@Massanori_Takaki thanks for your suggestions. Will try it out and let u know if it worked for me.

@Massanori_Takaki Looks like // was the issue.It worked.

Nice, also I would suggest you to check the link from w3school it helps a lot.

Hi,@Massanori_Takaki I have same issue with my application how could i select the next button

@rini

Use any one of the following(syntax is for Javascript, change the syntax as per your binding language(Ids and xpaths will remain same)):

driver.elementById(‘com.path.eniwhere:id/btn_next’)

driver.elementByXPath("//android.widget.Button[contains(@text, ‘NEXT’)]")

driver.elementByXPath("//android.widget.Button[contains(@resource-id, ‘com.path.eniwhere:id/btn_next’)]")

driver.elementByXPath("//android.widget.Button[contains(@resource-id, ‘com.path.eniwhere:id/btn_next’) and @text=‘NEXT’]")

1 Like

HI @shashikumarraja i have tried all this but sti getting same error

FAILED: testregistration
org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 81 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: ‘3.3.1’, revision: ‘5234b32’, time: ‘2017-03-10 09:04:52 -0800’
System info: host: ‘administrator-desktop’, ip: ‘127.0.1.1’, os.name: ‘Linux’, os.arch: ‘amd64’, os.version: ‘4.4.0-21-generic’, java.version: ‘1.8.0_121’
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{app=/home/Rupel/workspace/myappium/eniwhere/app-release_01_02.apk, appPackage=com.path.eniwhere, networkConnectionEnabled=true, warnings={}, appWaitPackage=com.path.eniwhere, appWaitActivity=com.path.eniwhere.activity.SplashActivity, databaseEnabled=false, deviceName=192.168.58.101:5555, platform=LINUX, deviceUDID=192.168.58.101:5555, appActivity=com.path.eniwhere.activity.SplashActivity, desired={app=/home/Rupel/workspace/myappium/eniwhere/app-release_01_02.apk, appPackage=com.path.eniwhere, platformName=Android, deviceName=Android, app Activity=.activity.LoginActivity}, platformVersion=5.1, webStorageEnabled=false, locationContextEnabled=false, takesScreenshot=true, javascriptEnabled=true, platformName=Android, app Activity=.activity.LoginActivity}]
Session ID: 29d082a4-2557-463d-ae3c-7c58abaeea24
*** Element info: {Using=xpath, value=//android.widget.Button[contains(@text, ‘NEXT’)]}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:638)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:371)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:62)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:476)
at io.appium.java_client.DefaultGenericMobileDriver.findElementByXPath(DefaultGenericMobileDriver.java:140)
at io.appium.java_client.AppiumDriver.findElementByXPath(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElementByXPath(AndroidDriver.java:1)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:58)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
at defaultproject.registration.testregistration(registration.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

and my code is like this

i am using java language

@rini
From your last screenshot, you should’ve used By.xpath("//android.widget.Button[contains(@text, ‘Next’)]") - It’s case sensitive.

From @shashikumarraja reply, I would suggest something like:
dr.findElement(By.id(“btn_next”)); //No need to add the package name
if you really want to use xpath, you can try:
dr.findElement(By.xpath("//android.widget.Button[contains(@resource-id, ‘btn_next’)]")); //If you use contains, there’s no need to add the package as well, just the id should be enough.
dr.findElement(By.xpath("//android.widget.Button[contains(@resource-id, ‘btn_next’) and @text=‘Next’]"));

Quick question, is the Next button displayed when you try to find it? (That might explain why it failed, in case it’s not displaed you’ll need to scroll before you try to find it)

@Massanori_Takaki thanks for this but have one issue more that i have to select “MS” in my drop down list
i was selecting by id but “Mr”, Ms, Mrs having id =text1
unable to select Ms please help me out

@rini
You can try to use something like this:
By.xpath("//android.widget.TextView[@text=‘Ms’]")
By.xpath("//android.widget.TextView[contains(@resource-id, ‘text1’) and @text=‘Ms’]")
or even a method that could return the Locator:

public By methodName(String aString) {
return By.xpath("//android.widget.TextView[@text=’"+aString+"’]");
}

FAILED: testregistration
org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 632 milliseconds
For documentation on this error, please visit: Selenium
Build info: version: ‘3.3.1’, revision: ‘5234b32’, time: ‘2017-03-10 09:04:52 -0800’
System info: host: ‘administrator-desktop’, ip: ‘127.0.1.1’, os.name: ‘Linux’, os.arch: ‘amd64’, os.version: ‘4.4.0-21-generic’, java.version: ‘1.8.0_121’
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{app=/home/Rupel/workspace/myappium/eniwhere/app-release_01_02.apk, appPackage=com.path.eniwhere, networkConnectionEnabled=true, warnings={}, appWaitPackage=com.path.eniwhere, appWaitActivity=com.path.eniwhere.activity.SplashActivity, databaseEnabled=false, deviceName=192.168.58.101:5555, platform=LINUX, deviceUDID=192.168.58.101:5555, appActivity=com.path.eniwhere.activity.SplashActivity, desired={app=/home/Rupel/workspace/myappium/eniwhere/app-release_01_02.apk, appPackage=com.path.eniwhere, platformName=Android, deviceName=Android, app Activity=.activity.LoginActivity}, platformVersion=5.1, webStorageEnabled=false, locationContextEnabled=false, takesScreenshot=true, javascriptEnabled=true, platformName=Android, app Activity=.activity.LoginActivity}]
Session ID: 8e5fb7f9-fe50-42d1-90f6-3dac8b4c9abc
*** Element info: {Using=id, value=et_fname}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:638)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:371)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:62)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:420)
at io.appium.java_client.DefaultGenericMobileDriver.findElementById(DefaultGenericMobileDriver.java:70)
at io.appium.java_client.AppiumDriver.findElementById(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElementById(AndroidDriver.java:1)
at org.openqa.selenium.By$ById.findElement(By.java:218)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:58)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
at defaultproject.registration.testregistration(registration.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0

===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0

not working this error is showing

When you try to find the Ms element, can you see the ListView with three options? (Mr, Ms and Mrs)
If can’t see it, you must first click on the Mr option to bring up the ListView, and then click on the Ms option.

dr. findElement(By.xpath("//android.widget.TextView[@text=‘Mr’]")).click(); //You can try to find a specific id for the component, I especify the TextView, because that’s the item visible.
dr. findElement(By.xpath("//android.widget.TextView[@text=‘Ms’]")).click();

thanks it is working

Cool, like I suggest shashikumarraja, if you want to know how to compose the xpath follow the link for the
w3 school.
It helps a lot, and sometimes I have to check again to remind me.
Also for Android, if the element is not displayed, you can not find it.

Hi @Massanori_Takaki

after this page i am not able to switch on another application is getting closed after few seconds can you please tell me what should i do now

Hey @rini
I think the Application is closing because the session has timed out, if you keep the session alive it should be fine.
I would suggest to use a WebDriverWait, which you can find more details in this page.
So, the usage of WebDriverWait gives you more methods that can help you, and also you could remove some of those Thread.sleep() from the code.
I tried to write a code that might help you. You need to replace the DesiredCapabilites code and the last line from test method, check the gone() method.

public class AndroidTest {
    private AppiumDriver dr;
    private WebDriverWait wDriver;

    @Before
    public void setUp() {
        DesiredCapabilities caps = new DesiredCapabilities();
        dr = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);
        wDriver = new WebDriverWait(dr, 65);
    }
    @Test
    public void test() { 
        find("et_company_code").sendKeys(" 30001");
        find"et_mobile_no").sendKeys("9540202589");
        find("tv_submit").click();
        find("et_company_code").sendKeys("123456");
        find("tv_submit").click();
        find("et_company_code").sendKeys("1234");
        find"et_mobile_no").sendKeys("1234");
        find("tv_submit").click();
        gone(dialog locator);//Look for the locator of the dialog displayed on the screenshot 
    }

    public WebElement find(String id) {
            return wDriver.until(ExpectedConditions.presenceOfElementLocated(By.id(id)));
    }

    public boolean gone(String id) {
            return wDriver.until(ExpectedConditions.invisibilityOfElementLocated(By.id(id)));
    }
}