Class HtmlUnitDriver.ElementsMap
- Enclosing class:
- HtmlUnitDriver
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddIfAbsent(HtmlUnitDriver driver, DomElement element) Returns an existingHtmlUnitWebElementwrapper for the givenDomElement, or creates and registers a new one if none exists.getWebElement(String elementId) Retrieves a previously registeredHtmlUnitWebElementby its element ID.voidRemoves all element mappings associated with the specifiedPage.
-
Constructor Details
-
ElementsMap
public ElementsMap()Creates a new, emptyElementsMap.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 Details
-
addIfAbsent
Returns an existingHtmlUnitWebElementwrapper for the givenDomElement, 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 owningHtmlUnitDriver; must not benullelement- the DOM element to wrap; must not benull- Returns:
- the existing or newly created
HtmlUnitWebElement
-
remove
Removes all element mappings associated with the specifiedPage.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 benull, in which case nothing is removed
-
getWebElement
Retrieves a previously registeredHtmlUnitWebElementby 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; nevernull - Throws:
org.openqa.selenium.StaleElementReferenceException- if no element is registered under the given ID
-