Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, if you're having problems with StaleElementReferenceExceptions it's because your tests are poorly written. It's a race condition. Consider the following scenario:</p> <pre><code>WebElement element = driver.findElement(By.id("foo")); // DOM changes - page is refreshed, or element is removed and re-added element.click(); </code></pre> <p>Now at the point where you're clicking the element, the element reference is no longer valid. It's close to impossible for WebDriver to make a good guess about all the cases where this might happen - so it throws up its hands and gives control to you, who as the test/app author should know exactly what may or may not happen. What you want to do is explicitly wait until the DOM is in a state where you know things won't change. For example, using a WebDriverWait to wait for a specific element to exist:</p> <pre><code>// times out after 5 seconds WebDriverWait wait = new WebDriverWait(driver, 5); // while the following loop runs, the DOM changes - // page is refreshed, or element is removed and re-added wait.until(presenceOfElementLocated(By.id("container-element"))); // now we're good - let's click the element driver.findElement(By.id("foo")).click(); </code></pre> <p>The presenceOfElementLocated() method would look something like this:</p> <pre><code>private static Function&lt;WebDriver,WebElement&gt; presenceOfElementLocated(final By locator) { return new Function&lt;WebDriver, WebElement&gt;() { @Override public WebElement apply(WebDriver driver) { return driver.findElement(locator); } }; } </code></pre> <p>You're quite right about the current Chrome driver being quite unstable, and you'll be happy to hear that the Selenium trunk has a rewritten Chrome driver, where most of the implementation was done by the Chromium developers as part of their tree.</p> <p>PS. Alternatively, instead of waiting explicitly like in the example above, you can enable implicit waits - this way WebDriver will always loop up until the specified timeout waiting for the element to become present:</p> <pre><code>driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS) </code></pre> <p>In my experience though, explicitly waiting is always more reliable. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload