Selenium : wait until attribute value changed

与世无争的帅哥 提交于 2020-01-06 10:21:59

问题


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

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

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""

Above code is giving error

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.


回答1:


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.

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


来源:https://stackoverflow.com/questions/52069683/selenium-wait-until-attribute-value-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!