首页 > 解决方案 > DOM 中不再存在缓存的元素

问题描述

就像在类似的问题中一样,我使用 appium + java。尝试选择元素

在移动应用程序中,我转到页面,然后有许多元素 android.widget.ImageView(0),我需要选择 6 个(例如)这样的元素并执行其他步骤。字节只能选择一个元素,然后得到这样的异常:

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);
    }

标签: javaandroidappium-desktop

解决方案


您可以使用显式等待来解决此问题。

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);
}

推荐阅读