5 Ways to Handle Hidden Web Elements in the UI Tree

Published on November 14, 2024

Debug & Inspect Disabled Elements

Have you ever tried locating a hidden element in the HTML DOM? It can be frustrating, when we have to mouse over a popover or tooltip element in a web app, just to see it disappear the moment we hover out and/or click on the Inspect icon in DevTools.

Hidden tooltip that is disabled in the DOM. ๐˜ˆ๐˜œ๐˜›: ๐˜ฉ๐˜ต๐˜ต๐˜ฑ๐˜ด://๐˜จ๐˜ฆ๐˜ต๐˜ฃ๐˜ฐ๐˜ฐ๐˜ต๐˜ด๐˜ต๐˜ณ๐˜ข๐˜ฑ.๐˜ค๐˜ฐ๐˜ฎ/๐˜ฅ๐˜ฐ๐˜ค๐˜ด/4.0/๐˜ค๐˜ฐ๐˜ฎ๐˜ฑ๐˜ฐ๐˜ฏ๐˜ฆ๐˜ฏ๐˜ต๐˜ด/๐˜ต๐˜ฐ๐˜ฐ๐˜ญ๐˜ต๐˜ช๐˜ฑ๐˜ด/.

We encounter a similar issue when we right-click on a dropdown element, when trying to inspect its nested options. The moment we remove focus from the element, its hidden options/subelements disappear from the HTML DOM. This is a common problem with Angular and React apps that use Bootstrap JavaScript features.

The challenge it to inspect a disappearing element and create a WebElement object with Xpath, CSS, or any other locator strategy, so that we can use that object in a test automation script.

Letโ€™s say we are working with a tooltip that appears upon a click and then disappears. But what if we want to debug this tooltip? For example, right-click on it and change its color? Well, we canโ€™t even focus on it for inspection. So what can we do?

There are several ways to freeze a page that will help us capture hidden elements and display them in the DOM. These approaches can be used with any UI automation toolโ€Šโ€”โ€ŠSelenium, Playwright, WebDriverIO, Cypress, etc.โ€Šโ€”โ€Šbecause testers locate web elements by using locator strategies that are agnostic of any particular tool or framework. For example, we might use Xpath in a Selenium script or CSS with Cypress. Common locator strategies, like Xpath, are supported by most UI test automation tools.

1. Press ๐…8 to Freeze Page

Hit F8 on Windows/Linux (or fn + F8 on macOS) to pause the page in the debugger.

Freeze the page (pause in debugger) with ๐…8 key

This keystroke hack is well-known among web testers. However, it doesnโ€™t always work.

2. Set ๐ƒ๐ž๐›๐ฎ๐ ๐ ๐ž๐ซ ๐“๐ข๐ฆ๐ž๐จ๐ฎ๐ญ in Console

If F8 doesnโ€™t work, open Chrome DevTools and run the following JavaScript code in the console. This will break into debugger mode in 5 seconds:

setTimeout(function(){debugger;}, 5000)
Run setTimeout JS command in the DevTools console to ๐˜—๐˜ข๐˜ถ๐˜ด๐˜ฆ ๐˜ช๐˜ฏ ๐˜‹๐˜ฆ๐˜ฃ๐˜ถ๐˜จ๐˜จ๐˜ฆ๐˜ณ

3. Remove ๐›๐ฅ๐ฎ๐ซ Event Listener

Another trick is to freeze the page by removing the ๐›๐ฅ๐ฎ๐ซ property under the Event Listeners tab.

CDP > Elements panel > Event Listeners tab > expand ๐›๐ฅ๐ฎ๐ซ property > ๐Ÿ—‘๏ธ delete event listeners

With the ๐›๐ฅ๐ฎ๐ซ property now gone, we can hit the Inspect icon and view elements that were previously disappearing.

4. Remove ๐Ÿ๐จ๐œ๐ฎ๐ฌ๐จ๐ฎ๐ญ Event Listener

Some of my QA counterparts experienced confusion, when they could not find the ๐›๐ฅ๐ฎ๐ซ property in their Event Listeners.

CDP > Elements panel > Event Listeners tab > expand ๐Ÿ๐จ๐œ๐ฎ๐ฌ๐จ๐ฎ๐ญ property > ๐Ÿ—‘๏ธ delete event listeners

For those who cannot find the ๐›๐ฅ๐ฎ๐ซ listeners, alternatively we can delete the ones in the ๐Ÿ๐จ๐œ๐ฎ๐ฌ๐จ๐ฎ๐ญ property.

Note that the deleted properties will re-appear the moment we refresh the page.

5. ๐„๐ฆ๐ฎ๐ฅ๐š๐ญ๐ž ๐…๐จ๐œ๐ฎ๐ฌ๐ž๐ ๐๐š๐ ๐ž in Styles

CDP > Elements panel > Styles tab > :hov > โœ… Emulate a focused page to display disabled elements

Chrome has a checkbox called ๐„๐ฆ๐ฎ๐ฅ๐š๐ญ๐ž ๐š ๐Ÿ๐จ๐œ๐ฎ๐ฌ๐ž๐ ๐ฉ๐š๐ ๐ž. When we click on it, Chrome switches focus from the DevTools to the current page.

Now we can actually inspect it in a much easier way. We no longer need weird hacks to debug disappearing elements on the page.

๐“—๐’ถ๐“…๐“…๐“Ž ๐“‰๐“ฎ๐“ˆ๐“‰๐’พ๐“ƒ๐“ฐ ๐’ถ๐“ƒ๐’น ๐’น๐“ฎ๐’ท๐“Š๐“ฐ๐“ฐ๐’พ๐“ƒ๐“ฐ!

I welcome any comments and contributions to the subject. Connect with me on LinkedIn, X , GitHub, or Insta. Check out my website.

If you find this post useful, please consider buying me a coffee.