Jean Lumen
Jean Lumen

Reputation: 49

Element not found in the cache - perhaps the page has changed since it was looked up -why?

I am new in this field and I would like to ask how can i loop if I am trying to achieve this scenario: (JAVA CODE THAT INVOLVES SELENIUM AND WEBDRIVER)

Here is the block of codes i want to execute if the "Click for more accounts" is not anymore present in the listview

    List<WebElement> button = driver.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button"));
    for(WebElement click:button){
        while(click.isDisplayed()){

            WebDriverWait JEAN11 = new WebDriverWait(driver, 100);
            JEAN11.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button")));
            click.click();
            Thread.sleep(4000);

        }
        WebElement present = driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody"));
        List<WebElement> list = present.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr"));
        System.out.println("Total Number of TR: " + list.size());

    }

Why am I receiving Element not found in the cache - perhaps the page has changed since it was looked up :(

Upvotes: 1

Views: 220

Answers (1)

Nguyen Vu Hoang
Nguyen Vu Hoang

Reputation: 1570

I suggest you to

1 - separate check "Click for more accounts" and "Total Number of TR:"

2 - spy new objects in this case. (Do not spy object just 1 time, then use it for your rest of script like you did)

So the code should be

while(clickMore == true) {
    List<WebElement> button = driver.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button"));
    if(button.size() > 0) {
        button.get(0).click();
        Thread.sleep(4000);
    }
    else clickMore = false;
}

WebElement present = driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody"));
List<WebElement> list = present.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr"));
System.out.println("Total Number of TR: " + list.size());

Upvotes: 1

Related Questions