1

I looked everywhere and saw many solutions but unfortunately in my case, it's not a matter of waiting for the visibility of elements.

Basically I am analyzing a webpage where the contents are updated at certain internals, so the numbers of the webpage changes. But the number of elements I am querying are constant. So 10 elements in total. I am constantly querying these elements but very quickly I am getting "StaleElementReferenceException" because I assume in the middle of me calling FindElement or FindElements and me accessing .Text property, the item is stale.

I tried Try/Catch also but no luck. I don't care if the element is stale, I will get it next time as I am constantly reading them.

How can I fix this issue?

Joan Venge
  • 315,713
  • 212
  • 479
  • 689
  • 1
    With as much rep as you have, you should know better than to post a question with no code or HTML whatsoever. A link to the page would be better. You should spend some time reading and understanding what a stale element is because it has nothing to do with visibility. – JeffC Jan 13 '18 at 05:56
  • 1
    @RAJ Great... where's your answer if the question is so clear and you clearly aren't a beginner with Selenium? – JeffC Jan 13 '18 at 06:47
  • 1
    @JeffC: With as much ego as you have, you should know better than to post a comment with nothing helpful. An answer would be better. You should spend some time reading and understanding English along with what this question outlines because it has nothing to do with visibility. – Joan Venge Jan 13 '18 at 07:35
  • Possible duplicate of [StaleElementReference Exception in PageFactory](https://stackoverflow.com/questions/44838538/staleelementreference-exception-in-pagefactory) – undetected Selenium Jan 13 '18 at 09:43
  • 2
    @JoanVenge Can you please add the code trials ? – NarendraR Jan 13 '18 at 13:26
  • 2
    @JoanVenge Sentence #1 in your question... `it's not a matter of waiting for the visibility of elements.` So if it has nothing to do with visibility, why are you talking about visibility? From your question it doesn't sound like you understand why/when stale elements happen. If you do, you should rephrase your question to make that more clear. You still haven't posted any code that shows how/when you get the exception so how are we supposed to answer this? Post a [mcve] as is required on SO. – JeffC Jan 13 '18 at 14:29
  • @Joan Venge, the recommended way to handle this exception is retry to locate the element and to perform the action until success. But if the text you are trying to get is refreshed quicker than the time is takes to execute a Selenium command (~25ms), then you'll have to locate the element and get the text with a scrip injection: `return document.querySelector(...).innerText;`. – Florent B. Jan 13 '18 at 18:40
  • @JoanVenge If you are ready to prefer for Selenium Ruby Binding then you can preferred to use WATIR(Wrapper around selenium Ruby Binding), this problem wouldn't even occur because WATIR regains the element whenever element goes to stale! – RAJ Jan 14 '18 at 14:49
  • @JeffC Oh If Question is clear then I must have known the answer as well? What? Dude pleaseeee! – RAJ Jan 14 '18 at 14:51
  • 1
    @RAJ No, but if you don't know the answer either then why are you insulting me and stating that I'm a beginner? – JeffC Jan 14 '18 at 19:50
  • @JeffC Once again I clearly know the answer but I haven't written here, And also I am not insulting instead you are insulting the OP who clearly raised the question. you must have known that the above question doesn't need a code sample if you have had enough of experience in selenium. – RAJ Jan 15 '18 at 06:32
  • 1
    @RAJ So you know the answer but aren't going to post it? So you are here to what? Insult people that ask questions and make comments? It makes a huge difference as to what the approach is and how the page works on how to solve stale elements. Regardless, one of the core principles of SO is post what you've tried. OP mentions that's he's tried some things but hasn't posted a single line of code. That's why this question is most likely going to get closed. – JeffC Jan 15 '18 at 14:20

3 Answers3

0

Stale element exception - tells you that your page was changed, but instance of your web element was not. It means when the HTML code of your page is changing you should find that element again, because html changed. That is how selenium works.

nick318
  • 575
  • 4
  • 18
0

Catching the exception works for me. Just find the element again in the catch block. The example is in Java but I'm sure you'll got the idea:

    public boolean isElementContentUpdated() {
    boolean isContentUpdated = false;

    try {
        isContentUpdated = checkElementContentIsUpdated(elementToCheck);
    } catch (StaleElementReferenceException e) {
        elementToCheck = waitForElement(getDriver().findElement(By));
        isContentUpdated = checkElementContentIsUpdated(elementToCheck);
    }

    return isContentUpdated ;
}

Of course you have to implement your method checkElementIsUpdated(elementToCheck)

plamenti
  • 109
  • 1
  • 4
0

If you're using PageObjects, create new instance at each interval so the elements get initialized again, and driver re-reads the dom.

E.g.

//Your page object get initialized along with it's elements
AccountPage accountPage = new AccountPage(); 

//Do stuff

//Interval passed, recreate the page to get new elements
accountPage = new AccountPage();