0

I have an HTML page that is using Knockout.js to populate some list.

An item is added to the list when I click a button.

I write Selenium WebDriver test:

  • I find the button, then click on it.
  • Then I find the new element.
  • Then I test the text of the new element.

I get a stale element exception.

If I wait after the click (paused in debugger, or adding a Thread.Sleep of about 500ms) the test passes.

How can I make sure the element I find isn't stale? Is there a property given to me by WebDriver I can test for that?

Itai Bar-Haim
  • 1,686
  • 16
  • 40
  • http://stackoverflow.com/questions/16166261/selenium-webdriver-stale-element-reference-exception?rq=1 – Arun Mar 16 '16 at 07:02

1 Answers1

0

You can define implicit wait to make sure the element exists before interacting with it

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

This will wait up two 10 seconds for the element to exists in the DOM

There might be additional JS changes, so you can use explicit wait with expected conditions

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.TextToBePresentInElementLocated(By.id("id"), "text"));
// do something with the element

This will wait up two 10 seconds for the element to contain the text.

Guy
  • 46,488
  • 10
  • 44
  • 88
  • I actually do something of that manner and I still get the StaleElementException when I try to access the element's text if I don't wait. That's why it's so weird to me. – Itai Bar-Haim Mar 16 '16 at 07:29
  • Is it possible knockout.js is changing the visibility/attachment of the elements it's adding several times while adding them? – Itai Bar-Haim Mar 16 '16 at 07:31
  • @ItaiBar-Haim Yes, it is. If I think the element is deleted from the DOM and than re-added to it. Try to use one of the text accepted conditions, for example `wait.until(ExpectedConditions.TextToBePresentInElementLocated(By.id("id"), "text"));` – Guy Mar 16 '16 at 07:36
  • I didn't really want to go there, as I don't think this is a robust solution (anyway, it feels wrong waiting for element's text), but unfortunately it seems this is the only way to go. Thanks. – Itai Bar-Haim Mar 16 '16 at 07:45
  • @ItaiBar-Haim The only other option I can think of is using `Thread.Sleep`. You need to decide wich one is the lesser evil. – Guy Mar 16 '16 at 07:47