# The Future of UI Element Targeting: Why FinalRun Beats XPath

## **Overview**

Robust, maintainable UI automation begins with precise element identification. This documentation introduces FinalRun’s JSON-based identifier framework and explores its advantages compared to traditional XPath-based selectors commonly used with Appium. Emphasizing relational identifiers, it guides automation engineers to build resilient, clear, and scalable tests for dynamic interfaces.

## **1\. Introduction to Element Identification**

In automated testing, the method used to find elements determines the reliability and maintainability of test suites. Two dominant approaches are:

* **XPath Selectors** (Appium): String-based queries that traverse the UI hierarchy.
    
* **FinalRun Identifiers**: Declarative, JSON-based objects combining attributes and relationships.
    

## **2\. XPath with Appium: Structure-Based Selection**

XPath is a query language for targeting nodes in an XML document, mapped to UI element structures in Appium.

### **Strengths**

* Universal; works across many platforms.
    
* Can express complex UI hierarchies.
    

### **Limitations**

* Fragile: Breaks with minor UI changes.
    
* Difficult Maintenance: Often requires updates after UI refactoring.
    
* Cryptic Syntax: Not intuitive; error-prone string manipulation.
    
* Performance: Deep or complex queries can slow tests.
    

## **3\. FinalRun Identifiers: Attribute and Relationship-Based Targeting**

FinalRun introduces a modern, JSON-driven approach, focusing on both the properties and relationships of UI elements.

### **Properties**

* `id`: Unique resource name.
    
* `text`: Displayed text.
    
* `type`: Element type (e.g., image).
    
* `accText`: Accessibility label.
    

### **Relationships (Relational Identifiers) — The Game Changer**

* `insideOf`: Specifies a parent container.
    
* `containsDescendants`: Element must have specified children.
    
* `rightOf`, `leftOf`, `topOf`, `bottomOf`: Spatial positioning. Not possible with Xpath
    
* `index`: 1-based (selects the nth match within context).
    

*Example Identifier:*

```json
{
  "text": "Submit",
  "insideOf": { "id": "footer" }
}
```

* Clearly identifies the element with **Submit** text inside the **footer** container. Works regardless of UI changes as long as the **Submit** text is inside the **footer** container.
    

Checkout the detailed documentation — [https://finalrun.gitbook.io/finalrun/advanced/element-identifier](https://finalrun.gitbook.io/finalrun/advanced/element-identifier)

## **What Makes Relational Identifiers Special?**

* **Contextual Targeting**: Define elements by their relation to others, not just absolute position in the hierarchy.
    
* **Resiliency**: Less impacted by UI layout changes.
    
* **Clarity**: Self-documenting JSON; easily readable/reusable across teams.
    
* **Disambiguation**: Combine relationships to uniquely identify elements in complex views.
    

> **📅** [Book a Demo](https://calendly.com/arnold-finalrun/30min)  
> *See how FinalRun fits into your existing workflow with a live Demo.*

## **5\. Practical Scenarios**

### **Scenario 1: Spatial Relationship (Icon Right of “Settings” Text)**

UI layout often positions elements side-by-side. For example, an icon may appear **visually to the right of** a “Settings” label. This spatial relationship is critical but not captured by hierarchy alone.

XPath: (Not possible)

```css
//android.widget.TextView[@text='Settings']/following-sibling::android.widget.ImageView
```

![](https://miro.medium.com/v2/resize:fit:500/1*VBQjuBXUsPPg5PkY7_knYA.jpeg align="center")

**Limitation with Xpath:** This only selects the image that comes *after* “Settings” in the view hierarchy — not necessarily to its right on screen.

FinalRun:

```json
{
  "type": "icon",
  "rightOf": { "text": "Settings" }
}
```

FinalRun uses actual screen coordinates to resolve spatial relationships, ensuring the icon is genuinely to the right of the “Settings” label — regardless of their order in the XML layout. XPath cannot guarantee this and often fails when layout shifts or rendering engines change order independently of the DOM.

### **Scenario 2: Parent Element Containing Specific Children**

Let’s say you want to identify a **section** that contains both a **“Title”** text and a **“Continue”** button.

XPath:

```css
//android.view.ViewGroup[.//android.widget.TextView[@text='Title'] and .//android.widget.Button[@text='Continue']]
```

FinalRun:

```json
{
  "containsDescendants": [
    { "text": "Title" },
    { "text": "Continue" }
  ]
}
```

FinalRun expresses the same logic in a much clearer, structured form — greatly improving readability and making the intent explicit. XPath handles this by nesting logical conditions in a dense string expression that is harder to maintain.

## **6\. Best Practices with FinalRun Identifiers**

* Prioritize unique attributes (`id`, `text`, `accText`, `type`).
    
* Add relational context (`insideOf`,`leftOf`, `topOf`, `bottomOf` `rightOf`) to eliminate ambiguity.
    
* Use indexing judiciously — only when necessary
    

## **7\. Conclusion**

FinalRun’s relational, spatial and property-driven identifiers significantly advance UI automation capabilities beyond XPath’s positional approach. By blending clear structure, human readability, and robust context-awareness, FinalRun maximizes both test reliability and developer productivity for today’s fast-moving interfaces.

Embrace relational identifiers to unlock a modern, resilient, and scalable automation strategy.

## **Related Reading**

Read our story behind why we took this new approach to element identification:

[How We Set Out to Solve the XPath Problem in Mobile UI Test Automation](https://hashnode.com/post/cmd8m76mq000802ie8dh0g08d)

[Why LLMs Like ChatGPT, Gemini, and Claude Understand FinalRun Identifiers Better Than XPath](https://hashnode.com/post/cmd8tlhm9001b02jp4g664kxu)

> **📅** [Book a Demo](https://calendly.com/arnold-finalrun/30min)  
> *See how FinalRun fits into your existing workflow with a live Demo.*
