> For the complete documentation index, see [llms.txt](https://help.autify.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.autify.com/nocode-web/en/when-the-test-doesnt-work/test-issues/how-to-grab-an-element-with-dynamic-data.md). Agents can also search and read this documentation through its [MCP server](https://help.autify.com/~gitbook/mcp).

# How to grab an element with dynamic data

This is a troubleshooting guide for "Element not found" errors where the target element has data/text that is different from when the scenario was recorded.

On this page, you'll learn how to stabilize your test execution against dynamic data.

## Cause

During test execution, the test execution engine sees the element's inner text to check if the element is the same as the recorded one. Although the element's identity is not solely determined by the text or data within it, dynamic data such as dates, names, and email addresses can cause an "Element not found" error.

## Solutions

### 1. Use a CSS selector

Using a CSS selector is the quickest workaround. Let's take a look at an example. In the screenshot below, we can see that the latest article is displayed at the top half of the page. When a new article is created, the title, date, and author will change, and older articles are moved down to the card-like list on the bottom half of the page.

![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-d8eccc95e54db919701e81c65012527a614df165%2Fnewest-title.png?alt=media)

To make sure that the title of the newest article was clicked, you can specify it like this:

```
div.LatestPost h1
```

![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-2a60872c7441c519e4b9818579407792f5cee099%2Fdynamic-data-css-selector-example.png?alt=media)

(To learn CSS selector basics, please see [Using CSS selectors effectively](https://help.autify.com/nocode-web/en/autify-university/advanced/locator-feature/how-to-use-css-selectors-effectively.md))

### 2. Update your website's structure

The test execution engine uses information in HTML to identify elements. You can make your tests more stable by making each element more unique. The most common fix is replacing general-purpose elements such as `span`, `div` with something more meaningful ("semantic" in technical terms).

Here are two examples:

#### ❌ HTML structure with less clues

```html
<div>
    <div>
        <div class="bold big">This is the page title</div>
        <div>
            Date: <span class="gray small">Jan 1, 2021</span>
        </div>
        <div>John</div>
    </div>
</div>
```

#### ✅ HTML structure with more clues

```html
<main>
    <article>
        <h1>This is the page title</h1>
        <p id="posted-at" aria-label="date" >
            Date: <time datetime="2021-01-01">Jan 1, 2021</time>
        </p>
        <p id="written-by" aria-label="author">John</p>
    </article>
</main>
```

Going back to the first example of our blog page, you can grab the title of the latest article without a CSS selector, even if a new article comes up. The title of the latest article is `h1`, i.e., the top-level header of the page, whereas the titles of older articles are `h3`. The test execution engine can use this difference to distinguish which title is that of the latest article.

![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-f447347d8ec46d1271f17ddac47a49edacb7670b%2Fno-css-selector.png?alt=media)

It may not be a low-cost solution to improve the structure of your website to make it more meaningful. However, it's worth trying, especially if you cannot find an effective CSS selector.

### Use a JS Step

You can query an element that has expected text like below:

```javascript
const expectedText = "Dec 15, 2021";
const allSpanElementsInPage = document.querySelectorAll("span");

// You need to convert the NodeList returned by querySelector to an Array
// to use the filter method.
const filteredElements = Array.from(allSpanElementsInPage).filter(
    (el) => {
        return el.innerText === expectedText;
    }
);

if (filteredElements.length === 1) {
    // Perform action as needed. For example, clicking.
    filteredElements[0].click();
} else if (filteredElements.length === 0) {
   throw new Error("No elements found with JS")
} else {
   throw new Error(`Could not determine single element. ${filteredElements.length} found.`)
}
```

## Related articles

* To learn CSS selector basics, see [Using CSS selectors effectively](https://help.autify.com/nocode-web/en/autify-university/advanced/locator-feature/how-to-use-css-selectors-effectively.md).
* To learn the definition of the term "Semantics", see [MDN Web Docs Glossary - Semantics](https://developer.mozilla.org/en-US/docs/Glossary/Semantics)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.autify.com/nocode-web/en/when-the-test-doesnt-work/test-issues/how-to-grab-an-element-with-dynamic-data.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
