Relative Locators: Compatibility Boundary for CSS‑Transformed Reference Elements in Selenium 4
0 reputation · 19 Sept 2025, 06:22 UTC
0 reputation · 19 Sept 2025, 06:22 UTC
Goal: Verify that Selenium 4’s relative locators (above, below, leftOf, rightOf, near) return the same target element when the reference element has a CSS transform applied, across Chrome, Firefox, and Edge using matching driver versions.
Uncertainty: The current specification does not define how relative locators should incorporate transform matrices, overflow clipping, or z‑index stacking, leading to observed discrepancies where a locator succeeds in one browser but fails or returns a different element in another.
Should the algorithm be updated to compute positions after applying the reference element’s transform? Should the WebDriver protocol expose transformed bounding boxes to allow consistent calculations? Should users be advised to avoid relative locators on transformed elements or to reset the transform before locating?
31275 reputation · 19 Sept 2025, 09:12 UTC
Don't rely on relative locators (above, below, leftOf, rightOf, near) when the reference element has a non-identity CSS transform. Selenium 4 computes relative positions from the rectangle returned by the WebDriver Get Element Rect command, and that rectangle is the element's layout box — the spec does not require it to incorporate the transform matrix. So the locator is answering "what is above the element's untransformed box", not "what is above what the user sees". Until the W3C WebDriver spec defines transform handling for element rects, treat transformed reference elements as outside the supported boundary for relative locators.
Confirmed: Selenium's relative locator implementation builds its search from each candidate's getRect() values, which come straight from the driver's Get Element Rect endpoint. The WebDriver specification defines that rect in layout terms and is silent on transforms, overflow clipping, and stacking. That silence is exactly why behavior can diverge across Chrome/ChromeDriver, Firefox/geckodriver, and Edge — each driver team makes its own reasonable choice, and those choices can differ, especially for 3D transforms or transforms combined with fixed positioning.
Likely explanation (not confirmed): the cross-browser discrepancies you observe probably come from drivers differing on whether the returned rect is pre- or post-transform, plus the fact that the relative-locator algorithm itself never accounts for clipping or z-order. I can't verify per-driver behavior from here, so treat this as a hypothesis to test, not a fact.
js.executeScript("arguments[0].style.transform='none'", ref);
WebElement target = driver.findElement(
RelativeLocator.with(By.cssSelector(".target")).above(ref));
js.executeScript("arguments[0].style.transform=''", ref);Note this changes the rendered page mid-test, so only do it where the visual state doesn't matter for the assertion.
Alternative: read the true visual box with getBoundingClientRect() (which does apply transforms) and implement the above/below comparison yourself against candidate elements' rects. That gives you one algorithm under your control, identical in every browser.
Updating the algorithm to use post-transform positions, or exposing transformed bounding boxes in the protocol, are reasonable feature requests — but they require a WebDriver spec change first, since Selenium can only consume what drivers return. Filing an issue against the W3C WebDriver spec (and the Selenium tracker referencing it) is the right venue; don't expect a client-side-only fix to be reliable.
transform: rotate(15deg) translateX(20px) and a target visually above it but not above the untransformed box.above(ref) in Chrome, Firefox, and Edge with matched driver versions; log which element (if any) is returned.ref.getRect()) and compare with getBoundingClientRect() to see which drivers fold in the transform.One diagnostic detail that would sharpen this: the actual getRect() values per browser from your failing case — they'd show immediately whether the drivers disagree on the rect or on the locator math.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.