Class HtmlUnitDriver.ElementsMap

  • Enclosing class:
    HtmlUnitDriver

    protected static class HtmlUnitDriver.ElementsMap
    extends Object
    Maintains a bidirectional mapping between DomElement instances and their corresponding HtmlUnitWebElement wrappers.

    The mapping is stored per SgmlPage, allowing elements to be reused across driver operations and ensuring that each DOM element has a stable, unique HtmlUnitWebElement representation for the lifetime of its page.

    Element wrappers are automatically removed when the associated page is removed, preventing memory leaks. Page-level maps are stored in a WeakHashMap, allowing them to be reclaimed when their pages are no longer referenced.

    • Constructor Detail

      • ElementsMap

        public ElementsMap()
        Creates a new, empty ElementsMap.

        Each instance assigns unique element IDs starting from 1 and maintains internal lookup structures for both page-based mappings and global ID-based lookup.

    • Method Detail

      • addIfAbsent

        public HtmlUnitWebElement addIfAbsent​(HtmlUnitDriver driver,
                                              DomElement element)
        Returns an existing HtmlUnitWebElement wrapper for the given DomElement, or creates and registers a new one if none exists.

        The wrapper is stored in both:

        • the page-specific element map, keyed by the actual DOM element, and
        • a global ID-based map used for lookup by element identifier.

        Each new wrapper is assigned a unique integer ID.

        Parameters:
        driver - the owning HtmlUnitDriver; must not be null
        element - the DOM element to wrap; must not be null
        Returns:
        the existing or newly created HtmlUnitWebElement
      • remove

        public void remove​(Page page)
        Removes all element mappings associated with the specified Page.

        This method is typically invoked when a page is navigated away from or discarded, removing stale element references and freeing associated memory.

        Parameters:
        page - the page whose element mappings should be removed; may be null, in which case nothing is removed
      • getWebElement

        public HtmlUnitWebElement getWebElement​(String elementId)
        Retrieves a previously registered HtmlUnitWebElement by its element ID.

        If the element is no longer valid—typically because the page was replaced— this method throws a StaleElementReferenceException, matching Selenium semantics.

        Parameters:
        elementId - the string identifier of the element
        Returns:
        the associated HtmlUnitWebElement; never null
        Throws:
        org.openqa.selenium.StaleElementReferenceException - if no element is registered under the given ID