Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.
To click a nested item reliably, locate it under its parent, wait until the application has made it available, and click the actual interactive element. If expanding or selecting a node rerenders the tree, reacquire the parent and target instead of reusing detached WebElement references.
A reliable way to click a nested tree item
- Inspect the rendered DOM and identify a stable locator for the tree container, the relevant parent, and the control you intend to click.
- Wait for the tree and required node to reach the state needed for the next action. A page’s navigation readiness does not guarantee its JavaScript-driven tree is ready.
- Search for the target relative to the parent when that expresses its nesting more precisely than a document-wide lookup.
- Wait for the target to be visible and enabled, then use WebDriver’s native click.
- If an interaction rerenders the tree, wait for the old reference to become stale when appropriate, then find the new parent and target from their locators.
The key distinction is between a locator and a WebElement. A locator describes how to find an element in the current DOM; a WebElement is a reference to a particular element already found. A rerender can invalidate the reference even when the replacement looks identical.
Python example: find the child under its parent
This is a template, not a selector that works for every tree widget. Replace the example container selector and target text with attributes and markup from your application. The code uses Python’s Selenium expected conditions and a relative XPath lookup beneath the tree element.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
# Replace these locators with stable attributes from the real page.
tree = wait.until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "[data-tree]"))
)
target = wait.until(lambda d: tree.find_element(
By.XPATH,
".//button[normalize-space()='Target node']"
))
wait.until(EC.element_to_be_clickable(target)).click()
The descendant search uses tree.find_element(...), not driver.find_element(...). Its XPath begins with .//, making the query relative to the tree element. The target predicate assumes the application renders the clickable node as a button with that text; if it uses a link or another control, adjust the locator to match.
#1 Best Overall
- Compact Mouse: With a comfortable and contoured shape, this Logitech ambidextrous wireless mouse feels great in either right or left hand and is far superior to a touchpad
- Durable and Reliable: This USB wireless mouse features a line-by-line scroll wheel, up to 1 year of battery life (2) thanks to a smart sleep mode function, and comes with the included AA battery
- Universal Compatibility: Your Logitech mouse works with your Windows PC, Mac, or laptop, so no matter what type of computer you own today or buy tomorrow your mouse will be compatible
- Plug and Play Simplicity: Just plug in the tiny nano USB receiver and start working in seconds with a strong, reliable connection to your wireless computer mouse up to 33 feet / 10 m (5)
- Better than touchpad: Get more done by adding M185 to your laptop; according to a recent study, laptop users who chose this mouse over a touchpad were 50% more productive (3) and worked 30% faster (4)
The clickable condition checks that an element is visible and enabled. It is a useful starting point, not a guarantee against every application-specific transition or overlay. If the tree needs to expand before the target exists, perform that interaction first, wait for the relevant update, and then locate the child.
Choose locators from the real markup
Selenium supports CSS selectors and XPath as locator strategies. Prefer stable, meaningful attributes supplied by the application, and scope a lookup beneath a known parent when the parent-child relationship matters. XPath can express relationships and text conditions; CSS can be concise when the needed attributes are available. Neither is universally best.
- Inspect the current DOM to determine whether the row itself, a nested button, or a link receives clicks.
- Use application-owned identifiers or other stable attributes when available.
- Use relative XPath such as
.//...when searching beneath a WebElement. - Avoid generated positional indexes unless the page offers no better stable identifier; insertion or reordering can change which item an index selects.
- Do not assume a visual tree row has a particular ARIA role or HTML tag. The widget’s actual markup determines the correct target.
Wait for the application condition, not a guessed delay
JavaScript may add or change elements after navigation has reached a ready state. Selenium’s waiting strategies guide recommends synchronizing with the state needed for the next interaction rather than treating navigation completion as proof that the page is ready. Fixed sleeps can be too short on a slow run and waste time on a fast one.
Recommended Free Tools
Rank #2
- The next-generation optical HERO sensor delivers incredible performance and up to 10x the power efficiency over previous generations, with 400 IPS precision and up to 12,000 DPI sensitivity
- Ultra-fast LIGHTSPEED wireless technology gives you a lag-free gaming experience, delivering incredible responsiveness and reliability with 1 ms report rate for competition-level performance
- G305 wireless mouse boasts an incredible 250 hours of continuous gameplay on just 1 AA battery; switch to Endurance mode via Logitech G HUB software and extend battery life up to 9 months
- Wireless does not have to mean heavy, G305 lightweight mouse provides high maneuverability coming in at only 3.4 oz thanks to efficient lightweight mechanical design and ultra-efficient battery usage
- The durable, compact design with built-in nano receiver storage makes G305 not just a great portable desktop mouse, but also a great laptop travel companion, use with a gaming laptop and play anywhere
Use an explicit wait for the next action’s precondition: for example, the tree becoming visible, the child appearing, an expansion indicator changing, or a prior element becoming stale. The right condition depends on how the specific widget signals its update. Do not mix implicit and explicit waits; Selenium warns that doing so can produce unpredictable wait times.
When the target is not present yet
Wait for a condition tied to the application update, then search again. If locating the child inside a lambda, be aware that the parent reference captured by that lambda can itself become stale if the whole tree is replaced. In that case, reacquire the parent as well as the child.
When the tree rerenders
If clicking an expander replaces the tree DOM, a saved WebElement may refer to a node that is no longer attached. Selenium’s Python expected-conditions API includes a staleness condition. You can wait for the old element to detach and then locate the replacement using its locator:
Rank #3
- Compact Mouse: With a comfortable and contoured shape, this Logitech ambidextrous wireless mouse feels great in either right or left hand and is far superior to a touchpad
- Durable and Reliable: This USB wireless mouse features a line-by-line scroll wheel, up to 1 year of battery life (2) thanks to a smart sleep mode function, and comes with the included AA battery
- Universal Compatibility: Your Logitech mouse works with your Windows PC, Mac, or laptop, so no matter what type of computer you own today or buy tomorrow your mouse will be compatible
- Plug and Play Simplicity: Just plug in the tiny nano USB receiver and start working in seconds with a strong, reliable connection to your wireless computer mouse up to 33 feet / 10 m (5)
- Better than touchpad: Get more done by adding M185 to your laptop; according to a recent study, laptop users who chose this mouse over a touchpad were 50% more productive (3) and worked 30% faster (4)
from selenium.webdriver.support import expected_conditions as EC
old_tree = tree
# Trigger the action that causes the application to replace or refresh the tree.
wait.until(EC.staleness_of(old_tree))
tree = wait.until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "[data-tree]"))
)
target = wait.until(lambda d: tree.find_element(
By.XPATH,
".//button[normalize-space()='Target node']"
))
wait.until(EC.element_to_be_clickable(target)).click()
Use this pattern only when the update is expected to detach the old tree. If the application updates the existing DOM in place, wait for the actual changed state instead of waiting for an element that will remain attached.
Common failures and how to diagnose them
| Symptom | Likely cause | What to check or change |
|---|---|---|
NoSuchElementException |
The locator did not match at lookup time, the update has not happened, or the search is rooted at the wrong parent. | Confirm the locator against the current DOM, verify the parent scope, and wait for the application state that makes the target appear. |
| Element is not clickable or interactable | The target may not be displayed or enabled, or an application-specific transition may still be underway. | Wait for visibility and enabled state; inspect whether an overlay or animation is active and identify the page-specific condition that ends it. |
StaleElementReferenceException |
The saved WebElement was detached when the tree refreshed or rerendered. | If the old node should be removed, wait for staleness, then reacquire the parent and child from locators. |
| Click intercepted | Another element may be covering the target, or the click is aimed at a non-interactive part of the row. | Inspect the rendered page, confirm the actual control intended to receive the click, and wait for any obstruction to clear. There is no universal tree-specific workaround. |
| Test is flaky with fixed sleeps | The pause does not correspond to the application’s actual readiness and varies with runtime conditions. | Replace it with an explicit wait for the state required by the next action. Avoid combining implicit and explicit waits. |
For reference, Selenium’s Python expected-conditions API documents clickability as visibility plus enabled state, as well as the staleness condition. The JavaScript WebElement API documents descendant lookup and element clicks; the underlying parent-scoped approach is applicable beyond JavaScript, though the sample above is specifically Python.
What to adapt for your tree widget
- Expansion model: determine whether the child is already in the DOM but hidden, or is inserted only after its parent is expanded. Wait for the corresponding visible or present state.
- Click target: identify the actual button, link, or other interactive control. Do not assume the whole row is clickable.
- Refresh behavior: determine whether expanding a node updates the existing markup or replaces the tree. The former calls for a changed-state wait; the latter may call for staleness and reacquisition.
- Text and localization: exact visible-text XPath is convenient for an example, but text may change with localization or duplicate labels. Stable application attributes can make the locator more robust.
- Timeout: choose an explicit-wait timeout suitable for the test environment and the operation. The example’s 10 seconds is illustrative, not a Selenium requirement or a guarantee that every application will update within that period.
Or skip the browser setup
If you need a screenshot of a page or a tree state rather than a WebDriver click interaction, ScreenshotNeo offers a one-request screenshot API and an MCP server for AI agents. It is not a replacement for testing a click; it can capture a page for inspection without setting up a browser automation flow.
Rank #4
- Computer mouse for easily navigating a computer interface; click, scroll, and more
- USB-A wired connection; if existing device only supports USB-C, an additional adapter will be required
- High-definition (1000 dpi) optical tracking ensures responsive cursor control for precise tracking and easy text selection
- 3 buttons offer effortless fingertip control
- Plug-and-go ready for instant use
For example, this cURL request returns a screenshot for the supplied URL. See the ScreenshotNeo API documentation for request options.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
ScreenshotNeo accepts cookie banners before capture and removes known consent platforms, newsletter popups, and chat widgets; those steps can be turned off. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, and responses identify page verdict and billing status in headers. AI agents can use its MCP server tools, including take_screenshot, get_page_info, and capture_pdf. The free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.More Selenium references
See Selenium’s locator strategies documentation for the supported locator approach, alongside the waits and API references linked above.
Best Value
- 【Plug and Play for Home/Office/School】The wireless computer mouse features 2.4GHz connectivity, delivering a stable, interference-free connection up to 32ft. Designed for 𝐦𝐞𝐝𝐢𝐮𝐦 𝐭𝐨 𝐥𝐚𝐫𝐠𝐞 𝐬𝐢𝐳𝐞𝐝 𝐡𝐚𝐧𝐝𝐬, it ensures comfortable use all day. Simply plug in the USB-A receiver for instant pairing—no drivers needed. 📌📌 If the mouse isn’t suitable, place the USB receiver in the battery compartment and return both.
- 【3 Levels Adjustable DPI】This travel USB mouse offers 3 adjustable DPI settings (800, 1200, 1600), allowing you to customize sensitivity for precise design work. Effortlessly switch to match your task and elevate your productivity. 📌 Please remove the film at the bottom of the mouse before use.
- 【Effortless Browsing】Equipped with forward and backward buttons, this computer mice streamlines your workflow, making it easy to navigate through web pages and files with a simple click. 📌Side button does not work on Mac.
- 【Visible Indicator Light】 The pc mouse features a visual indicator for DPI levels and low battery alerts. The red light flashes once for 800 DPI, twice for 1200 DPI, and three times for 1600 DPI. When the battery level is below 10%, the light flashes red until the mouse is completely out of power.
- 【Click to Wake】With smart sleep mode, it saves power by standby after 10 inactive minutes, just 2-3 clicks to wake. This efficient design delivers 3x longer battery life than motion-wake mice. Engineered for durability, its buttons and scroll wheel are tested for 10 million clicks, ensuring long-term reliability and consistent performance.
Frequently Asked Questions
Does finding a nested element require XPath?
No. Selenium supports both CSS selectors and XPath; choose a strategy that matches stable attributes and relationships in the actual DOM.
Can I reuse a WebElement after the tree changes?
Only if the referenced node remains attached. A rerender may detach it, in which case reacquire the parent and target.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Clear out junk files and repair common Windows errors3Scan for outdated or missing drivers - takes under a minuteQuick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

