A Formal Look at Selenium’s NoSuchElementException
Tags: testing, ui, selenium, computerscience
Published: 2017-08-12 21:20:48

---
title: A Formal Look at Selenium’s NoSuchElementException
published: true
description: Or Why Test Automation Frameworks Sometimes Fail on Finding UI Elements
tags: testing,ui,selenium,computerscience
cover_image: https://thepracticaldev.s3.amazonaws.com/i/18koduu6qm6599ecupfv.jpeg
---

*Originally posted on [Medium](https://medium.com/@beatngu13/a-formal-look-at-seleniums-nosuchelementexception-bafce97df2e7).*

As a Selenium user, you probably know the story: Out of a sudden, your tests fail due to the infamous `NoSuchElementException`. But this isn't something that only happens with Selenium. Most other test automation frameworks have their own (fancy) names for it, but the underlying problem is always the same: a particular UI element cannot be found.

Back in 2016, I did a small review on several Swing-compatible capture & replay tools as part of a project work at my alma mater. During this work, I stumbled across [“An Extensible Heuristic-Based Framework for GUI Test Case Maintenance”](http://www.cs.umd.edu/~atif/pubs/McMasterMemonTESTBEDS2009.pdf) by Scott McMaster and Atif M. Memon. In this paper, the authors introduced a formal model for the described situation—the *GUI element identification problem*. The problem typically occurs in regression testing, hence, between version *n* and *n* + *i* of the system under test (SUT). (Note that these versions are not necessarily stable releases, every change that is built can cause the phenomenon.) Since McMaster and Memon seemed to focus on desktop applications, it affected the terminology they have chosen. For instance, a separate window within the SUT is denoted as set *W*, containing various actionable GUI elements *e*. If you come from a web application background, you would probably speak of pages and UI elements. Sometimes, you also want to assert or verify non-actionable elements such as labels to ensure they contain a certain string. Nonetheless, the concept is essentially the same. *W*’ is a subsequent version of *W*, in which the number of elements *e*’ might vary (i.e. |*W*| ≠ |*W*’|, if you are unfamiliar with this notation, have a look at these quick references on [logic](http://www.rapidtables.com/math/symbols/Logic_Symbols.htm) and [set theory](http://www.rapidtables.com/math/symbols/Set_Symbols.htm)). Now, the GUI element identification problem is defined as:

>For each actionable GUI element *e* in *W*, find a corresponding (possibly modified) element *e*’ in *W*’ whose actions implement the same functionality.

In order to do so, each element *e*, *e*’ ∈ *W* ∪ *W*’ must be assigned to exactly one of the following three sets:

* **Deleted:** elements from *W* with no corresponding elements in *W*’, therefore, they have been deleted in version *n* + *i*.

<img src="https://cdn-images-1.medium.com/max/1600/1*-C4604ZjmRx9MB-MjS4KbA@2x.png" width="50%"/>

* **Created:** elements from *W*’ without an assignment to elements in *W*, thus, they have been created in version *n* + *i*.

<img src="https://cdn-images-1.medium.com/max/1600/1*j28mAjTQsT96aG4aAuKhBA@2x.png" width="50%"/>

* **Maintained:** elements from *W* which are still in *W*’, but that may have been modified.

<img src="https://cdn-images-1.medium.com/max/1600/1*JcNFxfjJcqhH9V0Pvqe1Gg@2x.png" width="50%"/>

If a test case uses elements from set *D*, then the underlying script must be adapted since mandatory elements are missing in the new version. Elements from *C* can affect test cases as well; for example, such an element might change the given layout, causing a different XPath somewhere else. It is usually also up to the developer or tester to decide whether these new elements need to be tested. Consequently, the accurate computation of *M* is the main interest of the GUI element identification problem. According to the authors, this “generally requires heuristic approaches in the absence of a model where each element has a programmer-defined unique identifier.”

Selenium offers [various locator types](http://www.seleniumhq.org/docs/02_selenium_ide.jsp#locating-elements) (e.g. ID, name, or XPath), each with its own pros and cons. Additionally, more advanced test automation frameworks sometimes apply elaborate algorithms to not rely on a single locator. For instance, [ReTest](https://www.retest.de/)—the company I work for—embraces a novel paradigm called difference testing, where the complete UI state is being captured. As a result, all available attributes can be used at once. Another interesting approach is done by [Testim](https://www.testim.io/). They incorporate historical data with the aid of machine learning to rank the locators for each element individually, which stabilizes the tests over time.

Although these (and other) techniques lead to a quite robust element recognition, they are no silver bullet … and I doubt the current state of the art offers one. But they shift the burden of the GUI element identification problem from humans to machines—reducing the amount of `NoSuchElementException`s (or whatever they are called by your framework of choice) developers and testers have to deal with.