Class HtmlUnitDriver.ElementsMap
- java.lang.Object
-
- org.openqa.selenium.htmlunit.HtmlUnitDriver.ElementsMap
-
- Enclosing class:
- HtmlUnitDriver
protected static class HtmlUnitDriver.ElementsMap extends Object
Maintains a bidirectional mapping betweenDomElementinstances and their correspondingHtmlUnitWebElementwrappers.The mapping is stored per
SgmlPage, allowing elements to be reused across driver operations and ensuring that each DOM element has a stable, uniqueHtmlUnitWebElementrepresentation 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 Constructor Description ElementsMap()Creates a new, emptyElementsMap.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description HtmlUnitWebElementaddIfAbsent(HtmlUnitDriver driver, DomElement element)Returns an existingHtmlUnitWebElementwrapper for the givenDomElement, or creates and registers a new one if none exists.HtmlUnitWebElementgetWebElement(String elementId)Retrieves a previously registeredHtmlUnitWebElementby its element ID.voidremove(Page page)Removes all element mappings associated with the specifiedPage.
-
-
-
Method Detail
-
addIfAbsent
public HtmlUnitWebElement addIfAbsent(HtmlUnitDriver driver, DomElement element)
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
public void remove(Page page)
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
public HtmlUnitWebElement getWebElement(String elementId)
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
-
-