1

Using appium + selenium + java. Trying to wait for mobile element with specific text.

In mobile application I am login to page, and during login have android.widget.TextView(0) with text, so I am checking this text and if new element android.widget.TextView(0) with different text show up then I can go with other steps. When new element show up old one does not exists

For that using following code but then I can see

Cached elements 'By.clazz: android.widget.TextView' do not exist in DOM anymore 

Code:

public static MobileElement waitForElementText(AndroidDriver driver, int index, String text, int timeout) {

        List<MobileElement> mobileElement = null;

        do {

            mobileElement = new WebDriverWait(driver, timeout)
                    .until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("android.widget.TextView")))
                    .stream().map(element -> (MobileElement) element).collect(Collectors.toList());
            System.out.println("waiting for: " + text + " found: " + mobileElement.get(index).getText());
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } while (mobileElement.get(index).getText() != text);

        System.out.println(mobileElement.get(index).getText());

        return mobileElement.get(index);
    }

any ideas what am I doing wrong?

Karol
  • 165
  • 2
  • 6
  • 19

1 Answers1

0

Cached elements 'By.clazz: android.widget.TextView' do not exist in DOM anymore is happening because when you are trying to fetch the text, the element is not present on the page. So, instead of putting the while condition on checking the text, you should put the condition on the size of the element list. The size would become zero when the element is not present on the page.

Change the while condition to:

while (mobileElement.size()>0);
Sameer Arora
  • 4,439
  • 3
  • 10
  • 20
  • Hi Sameer Arora . When I use while (mobileElement.size()>0); loop does not ends. Application open expected page and finding new element which I waiting for but loop does not ends :( – Karol Oct 29 '19 at 15:19