0

I am in some kind of situation here.

I have a function 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.

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.

Now, in my test_run script, I have some other methods calling after editNewUser().

The thing happens is, this method executes the following commands in if block.

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

Stack Trace refers to the line if(el.getText().equals(UserName)).

Can you tell me why am I receiving this exception, event though the commands inside if block are executed.

4

1 回答 1

0

错误消息告诉您问题所在。一旦您在 for 循环中编辑了第一个“el”,页面就发生了变化,页面已经更新,因此 Selenium 失去了对“元素”列表中剩余元素的跟踪。

您需要找到另一种方法来跟踪您正在循环的元素。我过去使用的一种技术是创建一个自定义对象来表示项目,在您的情况下可能是“用户”。然后该对象就足够聪明,可以在页面上重新找到自己。

于 2016-05-31T16:03:32.897 回答