硒:等待直到属性值更改 [英] Selenium : wait until attribute value changed

查看:74
本文介绍了硒:等待直到属性值更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页中有图片,其src在一段时间后发生了变化.更改src后可能存在两个可能的值(时间可能会有所不同)img_src_success或img_src_failed

I have image in webpage whose src is changed after some time there can be two possible value after src is changed(time can vary) img_src_success or img_src_failed

我添加了以下代码,等待src更改但它无法正常工作并给出错误.

I have added below code to wait until src is changed but its not working and giving error.

 WebDriverWait wait = new WebDriverWait(driver, 120);
 wait.until(ExpectedConditions.attributeToBe(image_src, "src",img_src_success );  

其中image_src = WebElement
src =属性
img_src_success =字符串值"/src/image/success.png"img_src_running ="/src/image/failed.png"的字符串值"

where image_src = WebElement
src = attribute
img_src_success = String value "/src/image/success.png" img_src_running= String value for "/src/image/failed.png""

上面的代码给出了错误

org.openqa.selenium.StaleElementReferenceException:过时的元素reference:元素未附加到页面文档.

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document.

请提出我做错了什么或其他任何方式.

Please suggest what I m doing wrong or any other way to do this.

推荐答案

StaleElementException .再次执行 findElement 可能会解决此问题. ExpectedConditions.attributeToBe 有一个变体,它接受 By 定位符.使用该方法可以确保每次进行检查之前都可以检索到该元素,并且可以解决问题.

StaleElementException is thrown when either the element is deleted or detached from the DOM. Doing a findElement again might solve the issue. There is a variant of ExpectedConditions.attributeToBe that accepts a By locator. Using that ensures that the element is retrieved each time before the check is made and may resolve the issue.

您可以将 wait.until 与您自己的 ExpectedCondition 一起使用,后者可以每次获取元素,还可以检查 StaleElementReferenceException .如下所示:

You can use wait.until with your own ExpectedCondition that can fetch the element each time and also check for StaleElementReferenceException. Something like below:

    wait.until(new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver input) {
            try {
                WebElement deployed_row = Report_id
                        .findElement(By.xpath("//div[(@class = 'gridxRow') and (@rowindex = '0')]"));
                WebElement table = deployed_row.findElement(By.className("gridxRowTable"));
                WebElement image_src = table.findElement(By.xpath("//tbody/tr/td[2]/div/div/span/img"));
                return image_src.getAttribute("src").equals(img_src_success);
            } catch (StaleElementReferenceException e) {
                return false;
            }
        }
    });

这篇关于硒:等待直到属性值更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆