Splunk Observability Cloud

Browser Test - Deal with dynamic IDs, XPath, CSS class names in DOM

fkabiri
New Member

I have a question regarding creating browser tests in Synthetic Monitoring. The website I'm testing generates dynamic IDs for DOM elements, which makes it unreliable to use id attributes for actions like clicking buttons or links.

I attempted to use full XPath expressions instead, but the site frequently introduces banners (e.g., announcements) that alter the DOM structure and shift element positions, causing the XPath to break.

I'm wondering if there's a more resilient approach to locating elements. For example, is it possible to run a JavaScript snippet to search for an element by its visible text or attribute value, and then use that reference in subsequent steps or click on the element via the JavaScript? If so, how can I implement this?

Alternatively, are there best practices or recommended locator strategies for handling dynamic content in Synthetic browser tests?

Labels (1)
0 Karma

bishida
Splunk Employee
Splunk Employee

Yes, there is an "execute javascript" option for a step in your test.  You should be able to write some javascript to locate the element you want to click. Specifically the querySelector() or querySelectorAll() methods should help you do that.

0 Karma

fkabiri
New Member

Thank you!
I wasn’t aware that we have access to the document object when writing JavaScript in Synthetic scripts—does the documentation explicitly mention this? If so, I’d love to have link and take a closer look.


Here’s what worked for me:


In my scenario, the parent element of the target has a static ID. Leveraging that, I crafted a JavaScript snippet that locates and returns the ID of the element containing the visible text the test needs to click. In the worst-case scenario, we could adapt the same logic to search across the entire page if needed.

 

fkabiri_0-1754817836139.png

Here is the JavaCode:

// Get the parent element
const parentId = 'contentSection'
const searchText='sign up'

const parentElement = document.getElementById(parentId);
if (!parentElement) {
console.warn(`Parent element with ID "${parentId}" not found.`);
return null;
}

// Normalize search text for case-insensitive comparison
const normalizedSearch = searchText.trim().toLowerCase();

// Search all descendant elements
const childElements = parentElement.querySelectorAll('*');
for (let child of childElements) {
if (child.textContent.trim().toLowerCase() === normalizedSearch) {
return child.id || null; // Return null if the element has no ID
}
}
// No match found
return null;

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Automating Threat Operations and Threat Hunting with Recorded Future

    Automating Threat Operations and Threat Hunting with Recorded Future June 29, 2026 | Register   Is your ...

Keep the Learning Going with the New Best of .conf Hub

Hello Splunkers, With .conf26 getting closer, there’s already a lot of excitement building around this year’s ...

Splunk Community Badges!

  Hey everyone! Ready to earn some serious bragging rights in the community? Along with our existing badges ...