0

I am using Angular Material Tab, and all my elements become stale after switching to different tab (which has a clone grid). Instead of doing this for all 20 elements on my grid, is there a way to Refresh ALL elements on page, so my references are not stale?

We are trying to acquire values on the grid.

private By JobIdHeaderGrid => By.XPath("//span[text() = 'Process ID']");

stale element reference: element is not attached to the page document

try {
    WebElement date = driver.findElement(By.linkText(Utility.getSheetData(path, 7, 1, 2)));
    date.click();
}
catch(org.openqa.selenium.StaleElementReferenceException ex)
{
    WebElement date = driver.findElement(By.linkText(Utility.getSheetData(path, 7, 1, 2)));
    date.click();
}

enter image description here

8
  • is this line causing stale element reference date.click(); ? Commented May 13, 2021 at 17:22
  • it is getting stale, when I try to Find/get the element @cruisepandey Commented May 13, 2021 at 17:23
  • try to reload the UI using selenium and see if that works ? Commented May 13, 2021 at 17:24
  • that would work, however manager wants to verify tabs, without reload @cruisepandey Commented May 13, 2021 at 17:24
  • Can you post few element from both the tabs as HTML in text format ? Commented May 13, 2021 at 17:25

1 Answer 1

1

The short answer is no, there is no way to automatically refresh stale elements. Basically a stale element is one that is no longer attached to the DOM. That could be for multiple reasons... the page reloads, the browser is navigated away from and back to the page, etc. In your case, when you switch tabs, it sounds like it's treated like a page change which wipes out all of your references.

I would recommend not storing references but instead create methods that fetch and then use the element, e.g. click.

public void ClickDate()
{
    driver.findElement(By.linkText(Utility.getSheetData(path, 7, 1, 2))).click();
}

That way you should get rid of all your stale elements because you aren't fetching them, switching tabs and then back again, and then clicking the stored reference. You always fetch and then click immediately.

Sign up to request clarification or add additional context in comments.

7 Comments

there is a way, but you're not refreshing the stale element, you're refreshing the reference to the element. In this case it sounds like the previous action is causing an update to the DOM which is taking a little bit. So it finds the element, but by the time the click happens since the DOM is still updating that reference has gone bad. Putting this into one line would not help you still have to get the reference and then call the method on it. You also need a webdriverwait for this.
@pcalkins As to your first few sentences, your description does not match up with OP's description of what is happening. He stated he gets 20 references, clicks a tab, and then all references become stale... it's not a timing issue as you are claiming. The rest I'm not sure what you are saying... are you saying my answer is right/wrong/other?
The only way that my code could result in a stale element is if in the few ms between the time it gets the reference and clicks the element, it goes stale... possible but highly, highly unlikely. I have code like this all over my projects that I've been using for years and don't have those issues.
It would depend on how the site works. If the DOM is updated via javascript a webdriverwait is needed.... however that will only wait until the element is there... it won't know if the element is there but the DOM is still being updated. That's why the Stale Element exception was created. To let you know so that you can re-get the element. To be honest I didn't read his post very carefully (I was just looking at the code he posted), but the answer is still the same. Use WebDriverWait/Expected Conditions and catch Stale Elements if those are still being thrown.
So basically after a get()/navigate() Selenium waits for the page to load... or a non-javascript updating site's click. If the click doesn't generate a standard page load, but instead generates a JS action which results in a DOM update a webdriverwait is needed and it will only wait as long as necessary for that element to appear and/or meet the expected condition.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.