Java 缓存的元素不再存在于DOM中

Java 缓存的元素不再存在于DOM中,java,android,appium-desktop,Java,Android,Appium Desktop,和类似的问题一样,我使用appium+java。尝试选择元素 在移动应用程序中,我将进入页面,在这之后,有许多元素android.widget.ImageView(0),我需要选择6个(例如)这样的元素并执行其他步骤。Byt只能选择一个元素,然后得到这样的异常: org.openqa.selenium.StaleElementReferenceException:Cached元素'By.id:com.company:id/selector_view'不再存在于DOM中 public Galle

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

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

org.openqa.selenium.StaleElementReferenceException:Cached元素'By.id:com.company:id/selector_view'不再存在于DOM中

public GalleryPage选择照片来自相册(整数计数){
List photos=driver.findelementsbyd(elements.get(“来自图库相册的照片选择器”);
for(int i=0;i
我也有同样的问题。我认为这是因为每次单击照片时,DOM都会发生变化。因此,当您尝试单击第二张照片时,缓存的元素不再位于DOM上

尝试将照片放入for循环中,如下所示:

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);
}
public GalleryPage选择照片来自相册(整数计数){
for(int i=0;i
这样,每个周期都会从刷新的DOM中检索照片列表


顺便说一下,您没有检查计数是否大于照片列表大小,这可能会导致边界外异常或类似情况。

我也有同样的问题。我认为这是因为每次单击照片时,DOM都会发生变化。因此,当您尝试单击第二张照片时,缓存的元素不再位于DOM上

尝试将照片放入for循环中,如下所示:

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);
}
public GalleryPage选择照片来自相册(整数计数){
for(int i=0;i
这样,每个周期都会从刷新的DOM中检索照片列表


顺便说一下,您没有检查计数是否大于照片列表大小,这可能会导致边界外异常或类似情况。

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

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);
}
public GalleryPage选择照片来自相册(整数计数)
{
List photos=driver.findelementsbyd(elements.get(“来自图库相册的照片选择器”);
for(int i=0;i
您可以使用显式等待来解决此问题

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);
}
public GalleryPage选择照片来自相册(整数计数)
{
List photos=driver.findelementsbyd(elements.get(“来自图库相册的照片选择器”);
for(int i=0;i
尝试将enableMultiWindows appium功能设置为true

或者您可以尝试通过WebDriverWait查找元素。在Appium(C#)中,当驱动程序试图查找本机元素时,有时会抛出StaleElementReferenceException。因此,您可以忽略此异常并等待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;
}
public ReadOnlyCollection WaitElements(按选择器)
{
var wait=new WebDriverWait(this.Driver,new TimeSpan(0,0,10));
ReadOnlyCollection结果=空;
尝试
{
等待。直到(驱动程序=>
{
尝试
{
var元素=driver.FindElements(选择器);
if(elements.Any())
{
结果=要素;
返回true;
}
}
捕获(StaleElementReferenceException)
{
//忽略
}
返回false;
});
}
捕获(WebDriverTimeoutException)
{
抛出新的NoTouchElementException(“未找到元素”);
}
返回结果;
}
尝试将enableMultiWindows appium功能设置为true

或者您可以尝试通过WebDriverWait查找元素。在Appium(C#)中,当驱动程序试图查找本机元素时,有时会抛出StaleElementReferenceException。因此,您可以忽略此异常并等待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;
}
public ReadOnlyCollection WaitElements(按选择器)
{
var wait=new WebDriverWait(this.Driver,new TimeSpan(0,0,10));
ReadOnlyCollection结果=空;
尝试
{
等待。直到(驱动程序=>
{
尝试
{
var元素=driver.FindElements(选择器);
if(elements.Any())
{
结果=要素;
返回true;
}
}
捕获(StaleElementReferenceException)
{
//忽略
}
返回false;
});
}
捕获(WebDriverTimeoutException)
{
抛出新的NoTouchElementException(“未找到元素”);
}
返回结果;
}

欢迎来到StackOverflow@Vadim。虽然您的代码示例可能会解决这个问题,但解释如何以及为什么解决这个问题将真正有助于提高答案的质量。记住,你回答的问题是未来的读者,而不仅仅是现在提问的人。我建议您在回答中添加一个解释。欢迎来到StackOverflow@Vadim。而您的代码示例可能会解决这个问题