Locate a web element by its position relative to another already-found element using Selenium 4 Relative Locators
domain: selenium.dev · 6 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗
Steps
In Python, import locate_with from selenium.webdriver.support.relative_locator and By from selenium.webdriver.common.by
Locate the anchor element normally, e.g. password_field = driver.find_element(By.ID, 'password')
Build the relative locator: driver.find_element(locate_with(By.TAG_NAME, 'input').above(password_field))
Chain other spatial methods as needed: .below(), .to_left_of(), .to_right_of(), or .near() (defaults to elements within 50px)
The equivalent Java binding uses RelativeLocator.with(By.tagName(...)) with .above()/.below()/.toLeftOf()/.toRightOf()/.near()
Pass the resulting relative locator into find_element/find_elements like any other locator
Known gotchas
Relative locators use each browser's element bounding-box geometry, so results can be inconsistent with overlapping, hidden, or zero-size elements
near() only guarantees proximity within a default distance, not directional alignment — it is not a stricter version of above/below/left/right
The anchor element must already be located via a normal locator first; a bad anchor locator breaks the whole relative query
Give your agent this knowledge — and 15,500+ more routes
One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus:
claude mcp add --transport http waymark https://mcp.waymark.network/mcp
Need this verified for your stack — or a route we don't have yet?