Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appium checking if element is displayed

Tags:

appium

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?

like image 977
david Avatar asked Oct 19 '25 08:10

david


2 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!");
        }
like image 164
Al Imran Avatar answered Oct 22 '25 02:10

Al Imran


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;
    }
}
like image 45
Suban Dhyako Avatar answered Oct 22 '25 02:10

Suban Dhyako



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!