'Appium is able to see beyond what is displayed on screen

Appium is able to see and find elements that is not displayed on screen

I am trying to build a test automation project, I would like my driver to scroll down and then perform some operation. but for some reason appium is able to find element even without scrolling down . I am not sure how appium is able to identify element that is not on screen and is only visible to naked eye when you scroll down. Anyone with similar issue found a workaround ?

I am using ExpectedCondition.visibilityOF(element) to determine if element is vsible on screen

  public boolean verifyCoverage(String coverage, String value, String type) throws IOException, InterruptedException {
    int counter = 0;

    for (int i = 0; i < 15; i++) {
        AndroidElement element = (AndroidElement) driver.findElementByAndroidUIAutomator("UiSelector().textContains(\"" + coverage + "\")");
        //WebElement coverageOption= driver.findElementByXPath("//android.widget.Button[contains(text(),'"+coverage+"')]");
        if (AndroidUtilities.waitForVisibility(driver, element)) {

            return true;
        }
        else {
            System.out.println ("Cannot see");

            return false;
        }

    }

public static boolean waitForVisibility(AndroidDriver<WebElement> driver, AndroidElement AndroidElement){
    try{
        // driver.findElementByAndroidUIAutomator("UiSelector().resourceId(\""+targetResourceId+"\")");

        WebDriverWait wait = new WebDriverWait(driver, 60);
        wait.until(ExpectedConditions.visibilityOf(AndroidElement));
        boolean isElementPresent = AndroidElement.isDisplayed();
        return isElementPresent;    
    }catch(Exception e){
        boolean isElementPresent = false;
        System.out.println(e.getMessage());
        return isElementPresent;
    }

}


Solution 1:[1]

As an answer i would recommend you to use visibilityOfElementLocated instean of visibilityOf. Plus, if you want to check an element for the existence without getting exceptions, try to take that approach:

if (!((AndroidDriver)driver).findElementsByAndroidUIAutomator("UiSelector().textContains(\"" + coverage + "\")").isEmpty()) {
 //some logic when element is located
} else {
 //scroll to the particular element
}

Solution 2:[2]

You can try these two solution within the page it will able to scroll to the element and do your actions .

MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+element+"\").instance(0))"));


 MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textMatches(\"" + NumbersCount + "\").instance(0))"));
 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vault23
Solution 2 Snehal Gajbhiye