0

I have try all available solving

WebDriverWait wait6 = new WebDriverWait(driver, 500);       
wait6 .until(ExpectedConditions.presenceOfElementLocated(By.xpath("(//i[@class='material-icons'])[" + j + "]")));

I have application where I need to click on all item and get text of item name I am get Stale Element Reference Exception. I have try to put different method to resolve it but nothing working.

public void page(WebDriver driver, String Filtername) throws InterruptedException {
waitForElementPresent(driver, 60, sidenavbutton);
    click(driver, sidenavbutton);
    Thread.sleep(2000);
    click(driver, viewcopyportfolio);
    Thread.sleep(1000);
    click(driver, sidenavbutton);
    waitForElementPresent(driver, 30, porfoliosheader);
    clearText(driver, pagenumtextbox);
    Thread.sleep(1000);
    setText(driver, pagenumtextbox, Filtername);
    Thread.sleep(1000);
    List<WebElement> editicons1 = driver.findElements(By.xpath("//i[@class='material-icons']"));
        for (int j = 1; j <= editicons1.size(); j++) {
            editicons1 = driver.findElements(By.xpath("//i[@class='material-icons']"));
            String porfolioName = driver.findElement(By.xpath("(//mat-table//mat-row)[" + j + "]//mat-cell[2]")).getText();
//Added to fix Stale Element Exception
            WebElement editicon = driver.findElement(By.xpath("(//i[@class='material-icons'])[" + j + "]"));
//In click method attached code below this will loop for 5 times 
            click1(driver, editicon, porfolioName + " portfolio edit icon");
            Thread.sleep(1000);
            waitForElementPresent(driver, 30, buildportfolioheader);
 }
}

This code for click1 method

public void click1(WebDriver driver, WebElement element, String name) throws InterruptedException { 
     int attempts = 0;
        while(attempts < 5) {
    try {
        element.click();
        Add_Log.info("Successfully clicked on  " + name);
        Reporter.log("Successfully clicked on " + name);
        return;
    } catch (Exception e) {
        attempts++;
         Thread.sleep(500);
        try {
            JavascriptExecutor executor = (JavascriptExecutor) driver;
            executor.executeScript("arguments[0].click();", element);
            Add_Log.info("Successfully clicked on  " + name);
            Reporter.log("Successfully clicked on " + name);
            return;
        } catch (Exception e2) {
            Add_Log.info("Not able to click " + name);
            Reporter.log("Not able to click " + name);
            TestResultStatus.Testfail = true;
            Assert.fail("Not able to click " + name);
        }
    }

        }
}
ASSS
  • 69
  • 7
  • where you get the error? – ewwink Jan 03 '19 at 05:15
  • When I move to new page 2 or 3 I will run few objects on 5th or 6th element I get stale element exception – ASSS Jan 04 '19 at 16:50
  • @ DebanjanB please help I try this too for(int i=1;i<12;i++) { List editicons = driver.findElements(By.xpath("//i[@class='material-icons']")); //waitForElementPresent(driver, 90, webediticon); editicons.get(i).click(); not working still – ASSS Jan 04 '19 at 21:31

1 Answers1

1

" editicons1 = driver.findElements(By.xpath("//i[@class='material-icons']"));" This line in the loop doesn't look like it's needed, you just wanted the initial count, I don't see a reason to re load the list of elements.

The problem with this wait logic is that if the element already exists it will just sleep a second, see that the element is there and then continue, and from what I've seen, the next page could then start loading and then your script will be in a world of hurt.

Thread.sleep(1000);

waitForElementPresent(driver, 30, buildportfolioheader);

IF the element isn't already on the page, I would swap the explicit wait to come first. The reason for that is that presence of an element doesn't really mean a whole lot, the page could still be in motion, so a little bit of a sleep after the explicit wait (assuming this is one of the last elements to appear on the page) usually stabilizes flakey scripts.

waitForElementPresent(driver, 30, buildportfolioheader);

Thread.sleep(1000);

  • I try this still getting same stale element Exception – ASSS Jan 04 '19 at 16:48
  • for(int i=1;i<12;i++) { List editicons = driver.findElements(By.xpath("//i[@class='material-icons']")); //waitForElementPresent(driver, 90, webediticon); editicons.get(i).click(); not working this too – ASSS Jan 04 '19 at 21:32