Dhruv Bhatnagar
Dhruv Bhatnagar

Reputation: 141

Getting Exception : Element not found in the cache - perhaps the page has changed since it was looked up

I am reaching to a page after clicking on a link.I have not clicking anything on that page yet. Still, As soon as the page loaded it throws an error: Element not found in the cache - perhaps the page has changed since it was looked up

List<WebElement> securityGroup = driver.findElements(By.cssSelector("td[class='button-col']>a:nth-of-type(2)"));
System.out.println(securityGroup.size());

Thread.sleep(5000);

for(WebElement link:securityGroup) {
    String b= link.getAttribute("href");
    boolean a= b.contains(data0);

    if(a){
        System.out.println(b);
        Thread.sleep(5000);
        System.out.println("before clicking link");
        link.click();   

        //After this new page opens and above error comes.**
    }else {
        System.out.println("No match found");
    }
}

Thread.sleep(5000);

Select sel = new Select(driver.findElement(By.xpath("//select[@name='groupId']")));
System.out.println(sel.getOptions().toString());
sel.selectByValue("TEST");

Upvotes: 1

Views: 4048

Answers (3)

Guy
Guy

Reputation: 50899

When you click on link and redirected to another page the driver looses securityGroup. That's what causing the exception.

You need to relocate securityGroup each itreation

List<WebElement> securityGroup = driver.findElements(By.cssSelector("td[class='button-col']>a:nth-of-type(2)"));
int size = securityGroup.size(); 

for (int i = 0 ; i < size ; ++i) {
    securityGroup = driver.findElements(By.cssSelector("td[class='button-col']>a:nth-of-type(2)"));
    WebElement link = securityGroup.get(i);

    String b = link.getAttribute("href");
    boolean a = b.contains(data0);

    if(a) {
        System.out.println(b);
        Thread.sleep(5000);
        System.out.println("before clicking link");
        link.click();
    }
    else {
        System.out.println("No match found");
    }
}

Upvotes: 0

StrikerVillain
StrikerVillain

Reputation: 3776

This is because of the for loop. You are finding the securityGroup which is a list and you are iterating through the list. In this for loop, you look for a condition and if yes you proceed to click on the link. But the issue here is that the list iteration is not complete and the for loop continues. But it wont find the String b= link.getAttribute("href"); of the next iteration because you are on a new page.

Use a break to break the loop once the condition is satisfied.

if(a){
    System.out.println(b);
    Thread.sleep(5000);
    System.out.println("before clicking link");
    link.click();   
    break;
}else {
    System.out.println("No match found");
}

Upvotes: 1

Leon Barkan
Leon Barkan

Reputation: 2703

there is not enough time to load the page and take the element:

driver.findElement(By.xpath("//select[@name='groupId']"))

try to do ImplicitlyWait after you init the driver

driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

and Thread.sleep(5000); is bad idea using with selenium, for waiting you have selenium methods

Upvotes: 0

Related Questions