Appium checking if element is displayed

Viewed 9688

I'm using Appium for Android

the following works for clicking on the element

driver.findElement(By.xpath("//*[@resource-id='com.app.android:id/prelogin_signup']")).click();

But I am trying to check if an element is on the screen and I tried the following

if (driver.findElement(By.xpath("//*[@resource-id='com.app.android:id/prelogin_signup']")).isDisplayed()) {
    System.out.println("FOUND");
} else {
    System.out.println("NOT FOUND!");
}

but it returns an Exception saying

INFO: HTTP Status: '405' -> incorrect JSON status mapping for 'unknown error' (500 expected)
org.openqa.selenium.WebDriverException: Method is not implemented

How can I check to see if an element is on the screen?

3 Answers

You can try this, hope it helps

//If the element found, do as you want

if (driver.findElements(By.xpath("//*[@resource-id='com.app.android:id/prelogin_signup']")).size() > 0) {
            System.out.println("FOUND");
        } else {
            System.out.println("NOT FOUND!");
        }

you can surround your code with try catch block.

public boolean isElementDisplayed(){
    try{
        return driver.findElement(By.xpath("//*[@resource-id='com.app.android:id/prelogin_signup']")).isDisplayed();

    }catch(Exception e){
        //System.out.println(e);
        return false;
    }
}

you can also make the generic function to check if element is displayed.

public boolean isElementDisplayed(MobileElement element){
    try{
        return element.isDisplayed();

    }catch(Exception e){
        //System.out.println(e);
        return false;
    }
}

You may also try isDisplayed().

 if (demoPage.proceedButton().isDisplayed() == true) {
            System.out.println("BUTTON FOUND*****");
            TestUtil.click(cartPage.proceedButton(), 10);
        } else {
            System.out.println("PROCEED BUTTON NOT FOUND!*********");
        }

Note - I am using Page Factory model. Hence, demoPage.proceedButton() is used, you may use your locator in place of this declaration.

Related