简体   繁体   English

调试StaleElementReference异常-Selenium WebDriver

[英]Debugging StaleElementReference exception - Selenium WebDriver

I am in some kind of situation here. 我在这种情况下。

I have a function editNewUser(). 我有一个函数editNewUser()。

public void editNewUser() throws Exception
    {
        driver.findElement(adminModuleDD).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(searchRes));
        List<WebElement> elements = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(uNames));
        for(WebElement el : elements)
        {
            if(el.getText().equals(UserName))
            {
                el.click();
                wait.until(ExpectedConditions.visibilityOfElementLocated(editUserHeading));
                driver.findElement(editUser).click();
                WebElement status_Dropdown = driver.findElement(By.xpath("//select[@id='systemUser_status']"));
                Select status = new Select(status_Dropdown);
                status.selectByVisibleText("Enabled");
            }
        }   
    }

The UserName is a public string variable that gets value in another function, during creation of user. UserName是一个公共字符串变量,在创建用户期间会在另一个函数中获取值。

In this script, I am navigating to a page that contains list of users, I am storing all user names in List 'elements' and then iterating over each element in the list whose text matches with user name. 在此脚本中,我导航到包含用户列表的页面,将所有用户名存储在列表'elements'中,然后遍历列表中文本与用户名匹配的每个元素。

Now, in my test_run script, I have some other methods calling after editNewUser(). 现在,在我的test_run脚本中,我还有一些其他方法在editNewUser()之后调用。

The thing happens is, this method executes the following commands in if block. 发生的事情是,此方法在if块中执行以下命令。

if(el.getText().equals(UserName))
    {
        el.click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(editUserHeading));
        driver.findElement(editUser).click();
        WebElement status_Dropdown = driver.findElement(By.xpath("//select[@id='systemUser_status']"));
        Select status = new Select(status_Dropdown);
        status.selectByVisibleText("Enabled");
    }

But as soon as next method is called, it stops the execution and throws org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up 但是,一旦调用了下一个方法,它将立即停止执行并引发org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up

Stack Trace refers to the line if(el.getText().equals(UserName)) . 堆栈跟踪指向if(el.getText().equals(UserName))

Can you tell me why am I receiving this exception, event though the commands inside if block are executed. 你能告诉我为什么我会收到这个异常吗,尽管if块中的命令被执行了。

The error message is telling you the problem. 错误消息告诉您问题所在。 The page has changed once you edit the first "el" in your for loop, the page has been updated, so Selenium has lost track of the remaining elements in the "elements" list. 一旦您在for循环中编辑了第一个“ el”,页面便发生了变化,页面已更新,因此Selenium不再跟踪“ elements”列表中的其余元素。

You'll need to find another way to track the elements that you are looping through. 您将需要找到另一种方式来跟踪循环中的元素。 a technique that I've used in the past is create a custom object to represent the items, in your case perhaps "User". 我过去使用的一种技术是创建一个自定义对象来表示项目,在您的情况下可能是“用户”。 that object is then smart enough that it can re-find itself on the page. 那么该对象足够聪明,可以在页面上重新找到自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM