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

  1. In Python, import locate_with from selenium.webdriver.support.relative_locator and By from selenium.webdriver.common.by
  2. Locate the anchor element normally, e.g. password_field = driver.find_element(By.ID, 'password')
  3. Build the relative locator: driver.find_element(locate_with(By.TAG_NAME, 'input').above(password_field))
  4. Chain other spatial methods as needed: .below(), .to_left_of(), .to_right_of(), or .near() (defaults to elements within 50px)
  5. The equivalent Java binding uses RelativeLocator.with(By.tagName(...)) with .above()/.below()/.toLeftOf()/.toRightOf()/.near()
  6. Pass the resulting relative locator into find_element/find_elements like any other locator

Known gotchas

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?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans