StaleElementReferenceException in get text with [duplicate]












0
















This question already has an answer here:




  • Getting StaleElementReferenceException while trying print the link names

    3 answers



  • Selenium Webdriver - Stale element exception when clicking on multiple dropdowns while HTML DOM doesn't change

    1 answer



  • StaleElementReference Exception in PageFactory

    3 answers




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);
}
}

}
}









share|improve this question















marked as duplicate by DebanjanB selenium
Users with the  selenium badge can single-handedly close selenium questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 9:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • where you get the error?

    – ewwink
    Jan 3 at 5: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 4 at 16:50











  • @ DebanjanB please help I try this too for(int i=1;i<12;i++) { List<WebElement> editicons = driver.findElements(By.xpath("//i[@class='material-icons']")); //waitForElementPresent(driver, 90, webediticon); editicons.get(i).click(); not working still

    – ASSS
    Jan 4 at 21:31
















0
















This question already has an answer here:




  • Getting StaleElementReferenceException while trying print the link names

    3 answers



  • Selenium Webdriver - Stale element exception when clicking on multiple dropdowns while HTML DOM doesn't change

    1 answer



  • StaleElementReference Exception in PageFactory

    3 answers




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);
}
}

}
}









share|improve this question















marked as duplicate by DebanjanB selenium
Users with the  selenium badge can single-handedly close selenium questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 9:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • where you get the error?

    – ewwink
    Jan 3 at 5: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 4 at 16:50











  • @ DebanjanB please help I try this too for(int i=1;i<12;i++) { List<WebElement> editicons = driver.findElements(By.xpath("//i[@class='material-icons']")); //waitForElementPresent(driver, 90, webediticon); editicons.get(i).click(); not working still

    – ASSS
    Jan 4 at 21:31














0












0








0









This question already has an answer here:




  • Getting StaleElementReferenceException while trying print the link names

    3 answers



  • Selenium Webdriver - Stale element exception when clicking on multiple dropdowns while HTML DOM doesn't change

    1 answer



  • StaleElementReference Exception in PageFactory

    3 answers




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);
}
}

}
}









share|improve this question

















This question already has an answer here:




  • Getting StaleElementReferenceException while trying print the link names

    3 answers



  • Selenium Webdriver - Stale element exception when clicking on multiple dropdowns while HTML DOM doesn't change

    1 answer



  • StaleElementReference Exception in PageFactory

    3 answers




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);
}
}

}
}




This question already has an answer here:




  • Getting StaleElementReferenceException while trying print the link names

    3 answers



  • Selenium Webdriver - Stale element exception when clicking on multiple dropdowns while HTML DOM doesn't change

    1 answer



  • StaleElementReference Exception in PageFactory

    3 answers








java selenium selenium-webdriver webdriver staleelementreferenceexception






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 19:36







ASSS

















asked Jan 2 at 16:31









ASSSASSS

104




104




marked as duplicate by DebanjanB selenium
Users with the  selenium badge can single-handedly close selenium questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 9:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by DebanjanB selenium
Users with the  selenium badge can single-handedly close selenium questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 9:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • where you get the error?

    – ewwink
    Jan 3 at 5: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 4 at 16:50











  • @ DebanjanB please help I try this too for(int i=1;i<12;i++) { List<WebElement> editicons = driver.findElements(By.xpath("//i[@class='material-icons']")); //waitForElementPresent(driver, 90, webediticon); editicons.get(i).click(); not working still

    – ASSS
    Jan 4 at 21:31



















  • where you get the error?

    – ewwink
    Jan 3 at 5: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 4 at 16:50











  • @ DebanjanB please help I try this too for(int i=1;i<12;i++) { List<WebElement> editicons = driver.findElements(By.xpath("//i[@class='material-icons']")); //waitForElementPresent(driver, 90, webediticon); editicons.get(i).click(); not working still

    – ASSS
    Jan 4 at 21:31

















where you get the error?

– ewwink
Jan 3 at 5:15





where you get the error?

– ewwink
Jan 3 at 5: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 4 at 16:50





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 4 at 16:50













@ DebanjanB please help I try this too for(int i=1;i<12;i++) { List<WebElement> editicons = driver.findElements(By.xpath("//i[@class='material-icons']")); //waitForElementPresent(driver, 90, webediticon); editicons.get(i).click(); not working still

– ASSS
Jan 4 at 21:31





@ DebanjanB please help I try this too for(int i=1;i<12;i++) { List<WebElement> editicons = driver.findElements(By.xpath("//i[@class='material-icons']")); //waitForElementPresent(driver, 90, webediticon); editicons.get(i).click(); not working still

– ASSS
Jan 4 at 21:31












1 Answer
1






active

oldest

votes


















0














" 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);






share|improve this answer
























  • I try this still getting same stale element Exception

    – ASSS
    Jan 4 at 16:48











  • for(int i=1;i<12;i++) { List<WebElement> editicons = driver.findElements(By.xpath("//i[@class='material-icons']")); //waitForElementPresent(driver, 90, webediticon); editicons.get(i).click(); not working this too

    – ASSS
    Jan 4 at 21:32


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














" 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);






share|improve this answer
























  • I try this still getting same stale element Exception

    – ASSS
    Jan 4 at 16:48











  • for(int i=1;i<12;i++) { List<WebElement> editicons = driver.findElements(By.xpath("//i[@class='material-icons']")); //waitForElementPresent(driver, 90, webediticon); editicons.get(i).click(); not working this too

    – ASSS
    Jan 4 at 21:32
















0














" 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);






share|improve this answer
























  • I try this still getting same stale element Exception

    – ASSS
    Jan 4 at 16:48











  • for(int i=1;i<12;i++) { List<WebElement> editicons = driver.findElements(By.xpath("//i[@class='material-icons']")); //waitForElementPresent(driver, 90, webediticon); editicons.get(i).click(); not working this too

    – ASSS
    Jan 4 at 21:32














0












0








0







" 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);






share|improve this answer













" 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);







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 20:00









Dominic GiallombardoDominic Giallombardo

58757




58757













  • I try this still getting same stale element Exception

    – ASSS
    Jan 4 at 16:48











  • for(int i=1;i<12;i++) { List<WebElement> editicons = driver.findElements(By.xpath("//i[@class='material-icons']")); //waitForElementPresent(driver, 90, webediticon); editicons.get(i).click(); not working this too

    – ASSS
    Jan 4 at 21:32



















  • I try this still getting same stale element Exception

    – ASSS
    Jan 4 at 16:48











  • for(int i=1;i<12;i++) { List<WebElement> editicons = driver.findElements(By.xpath("//i[@class='material-icons']")); //waitForElementPresent(driver, 90, webediticon); editicons.get(i).click(); not working this too

    – ASSS
    Jan 4 at 21:32

















I try this still getting same stale element Exception

– ASSS
Jan 4 at 16:48





I try this still getting same stale element Exception

– ASSS
Jan 4 at 16:48













for(int i=1;i<12;i++) { List<WebElement> editicons = driver.findElements(By.xpath("//i[@class='material-icons']")); //waitForElementPresent(driver, 90, webediticon); editicons.get(i).click(); not working this too

– ASSS
Jan 4 at 21:32





for(int i=1;i<12;i++) { List<WebElement> editicons = driver.findElements(By.xpath("//i[@class='material-icons']")); //waitForElementPresent(driver, 90, webediticon); editicons.get(i).click(); not working this too

– ASSS
Jan 4 at 21:32





Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter