Not able to fix stale element exception - :Linux/Chrome 66/Chromedriver 2.37(Urgent)

121 views
Skip to first unread message

garvitag...@gmail.com

unread,
Jun 5, 2018, 1:39:13 AM6/5/18
to Selenium Users
Hi,

Suddenly i have starting hitting stale element exception for below scenario:

1. Getting element in list :
List<WebElement> list = driver.findElements(By.cssSelecto("ul#doc-list a.text-title"));
It return list of 4 elements.

2. getText() from list elements and add to set.
 Set<String> set = new HashSet<String>();

for (WebElement we : list) {
            set.add(we.getText());  // i am hitting stale element on this line always.
        }

Solutions tried :
1. Load list again before getting text
for (WebElement we : list) {
            list = driver.findElements(By.cssSelecto("ul#doc-list a.text-title"));
            set.add(we.getText()); 
        }

2. get list element in loop in case list is not loaded completely. 
for(int i=0;i<5;i++)
{
       list = driver.findElements(By.cssSelecto("ul#doc-list a.text-title"));
}

3. Add text from list element in another list, then add to set. 
List<String> text = new ArrayList();
for(WebElement s : list)
{
  li.add(s.getText();
}


Nothing worked at all. This stale element exception is not consistent, 2 times it runs successfully next time it hits error. This exception occurs only running in CI, on local i never hot it so i am not bale to debug it. It all started after upgrading to chrome 66.

Anyone has any solution to this, please advice same.

Thanks !!



Joe Ward

unread,
Jun 5, 2018, 6:58:29 AM6/5/18
to seleniu...@googlegroups.com
Without knowing what you're trying to test against or at seeing something like a dump of the DOM at the time that line is executed there's not much we can say that's not generic.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/cf8ad6a7-38cd-40b5-b266-333473efd1c3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Scott Babcock

unread,
Jun 13, 2018, 12:11:04 AM6/13/18
to Selenium Users
If all you're trying to do is extract text from a collection of elements, I recommend using a bit of JavaScript instead. You can get the entire set of values (not element references) in a single call, which is much more efficient.

Regardless, stale element reference failures indicate that the page is getting redrawn after you acquire the references. This indicates that the page may occasionally take longer to load, or that the page contains dynamic content that's changing the DOM, which necessitates periodic rebuilding.

Scott Babcock

unread,
Jun 13, 2018, 12:34:11 AM6/13/18
to Selenium Users
Alternatively, you could use my Selenium Foundation framework and make stale reference failures a thing of the past. This would require switching your tests to a new framework, which may be more effort than you're ready to expend, but you'll never see another stale reference "noise" failure like you're fighting now.


On Monday, June 4, 2018 at 10:39:13 PM UTC-7, garvitag...@gmail.com wrote:

total QA

unread,
Jun 13, 2018, 1:55:19 AM6/13/18
to Selenium Users
Hi,
Please find the solution for the issue as follow:

for (WebElement we : list) {
           
            set.add(we.getText()); 
           //Please add this line after add
           list = driver.findElements(By.cssSelecto("ul#doc-list a.text-title"));
        }

For advanced reporting for your project please refer to the links below:

Regards,

garvitag...@gmail.com

unread,
Jun 18, 2018, 1:44:38 AM6/18/18
to Selenium Users
Hi Scott,

Thanks for providing framework reference. I will have a look and will switch as i would surely not like to see these exceptions in long run.

Thanks !!

vamsi naga

unread,
Jun 18, 2018, 1:54:24 AM6/18/18
to seleniu...@googlegroups.com
I believe when the network/browser is slow in performance you are getting this error. Before the page fully load, you are retrieving the list of elements and when you are trying to retrieve the text of those elements you are facing the issue. Try to wait till the page loads, using some external waits and try to retrieve the elements.

Hope it works

Thanks ... Vamsi 

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Regards,
Naga Vamsikrishna Vaka.

total QA

unread,
Jun 18, 2018, 2:38:06 AM6/18/18
to Selenium Users
Hi,

you can even call findElements and insert a wait on it. This would be helpful. Else else get the focus of the element which is in the bottom of the page as mentioned below:

              new Actions(driver).moveToElement(WebElement);

Regards,



To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

garvitag...@gmail.com

unread,
Jun 20, 2018, 1:26:50 AM6/20/18
to Selenium Users
Hi Vamsi,

I added wait until all elements in  list are visible , it did not work too. 

Thanks,
Garvita 

On Monday, June 18, 2018 at 11:24:24 AM UTC+5:30, vamsi naga wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

garvitag...@gmail.com

unread,
Jun 20, 2018, 1:30:12 AM6/20/18
to Selenium Users
I tried both solutions, nothing is working.

I tried below too :

1. Ignoring stale element exception till gettext() is true:
WebDriverWait wait = new WebDriverWait(getDriverInstance(), 10);
            wait.ignoring(StaleElementReferenceException.class).until(
                    new ExpectedCondition<Boolean>() {
                        public Boolean apply(WebDriver driver) {
                            System.out.println("inside wait condition");
                            pathPatents.add(ele.getText());
                            return true;
                        }

2. Get text, not reference and find element in loop for first time
int patentlist = driver.findElements(By.cssSelector("ul#doc-list a.text-title"))
for(int i = 1; i <= patentlist; i++) {
            String locator = String.format(prop.getProperty("patentNo_CSS"), i);
            WebElement cell = driver.findElement(By.cssSelector(locator));
             pathPatents.add(cell.getText());
        }

Thanks,
Garvita
Reply all
Reply to author
Forward
0 new messages