3

Like in similar issue, I use appium + java. Trying to select elements

In mobile application I am go to page, and after that, have many elements android.widget.ImageView(0), I need to select 6 (for example) such elements and go with other steps. Byt can select only one element and then get such exception:

org.openqa.selenium.StaleElementReferenceException: Cached elements 'By.id: com.company:id/selector_view' do not exist in DOM anymore

public GalleryPage choosePhotosFromAlbum(int count) {
        List<MobileElement> photos = driver.findElementsById(elements.get("photo from gallery album selector"));
        for (int i = 0; i < count; i++) {
            photos.get(i).click();
        }
        return new GalleryPage(device);
    }
Aspirantis
  • 31
  • 1
  • 5

3 Answers3

1

I had the same issue. I think this happens because every time you click on a photo, the DOM changes. So, when you try to click on the second photo, the cached elements are not on the DOM anymore.

Try putting the photos inside the for cycle, like this:

public GalleryPage choosePhotosFromAlbum(int count) {

        for (int i = 0; i < count; i++) {
            List<MobileElement> photos = driver.findElementsById(elements.get("photo from gallery album selector"));            
            photos.get(i).click();
        }
        return new GalleryPage(device);
}

This way the list of photos is retrieved from the refreshed DOM on every cycle.

And by the way, you're not checking if the count is bigger than the photos list size, which can result in an OutOfBounds exception or similar.

NHK
  • 41
  • 7
0

You can use explicit wait to solve this problem.

public GalleryPage choosePhotosFromAlbum(int count) 
{
    List<MobileElement> photos = driver.findElementsById(elements.get("photo from gallery album selector"));              
        for (int i = 0; i < count; i++) 
        {
        new WebDriverWait(driver,10).until(ExpectedConditions.presenceOfAllElementsLocatedBy("Your object property"));
            photos.get(i).click();
        }
        return new GalleryPage(device);
}
0

Try enableMultiWindows appium capability set to true https://github.com/appium/appium/blob/master/docs/en/advanced-concepts/settings.md

Or you can try finding elements via WebDriverWait. In Appium (C#), when the driver tries to find native elements, it can sometimes throw a StaleElementReferenceException. Thus you can ignore this exception and wait until elements exist in DOM:

public ReadOnlyCollection<IWebElement> WaitElements(By selector)
{
    var wait = new WebDriverWait(this.Driver, new TimeSpan(0, 0, 10));
    ReadOnlyCollection<IWebElement> results = null;

    try
    {
        wait.Until(driver =>
        {
            try
            {
                var elements = driver.FindElements(selector);

                if (elements.Any())
                {
                    results = elements;
                    return true;
                }
            }
            catch (StaleElementReferenceException)
            {
                // ignore
            }

            return false;
        });
    }
    catch (WebDriverTimeoutException)
    {
        throw new NoSuchElementException("Elements not found");
    }

    return results;
}
Vadim
  • 1
  • 1
  • Welcome to StackOverflow @Vadim. While your code example may solve the question an explanation of how and why this solves the problem would really help to improve the quality of your answer. Remember that you're answering the question for readers in the future, not just the person asking now. I suggest you add an explanation to your answer. – Mike Poole Jan 14 '21 at 14:22