{"record":{"id":"ebeb110ac36049cb","repo":"SeleniumHQ/selenium","slug":"cannot-locate-relative-element-with-by-root","errorCode":null,"errorMessage":"Cannot locate relative element with: {by.root}","messagePattern":"Cannot locate relative element with: (.+?)","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"py/selenium/webdriver/remote/webdriver.py","lineNumber":907,"sourceCode":"        Args:\n            by: The locating strategy to use. Default is `By.ID`. Supported\n                values include: By.ID, By.NAME, By.XPATH, By.CSS_SELECTOR,\n                By.CLASS_NAME, By.TAG_NAME, By.LINK_TEXT, By.PARTIAL_LINK_TEXT,\n                or RelativeBy.\n            value: The locator value to use with the specified `by` strategy.\n\n        Returns:\n            The first matching WebElement found on the page.\n\n        Example:\n            `element = driver.find_element(By.ID, 'foo')`\n        \"\"\"\n        by, value = self.locator_converter.convert(by, value)\n\n        if isinstance(by, RelativeBy):\n            elements = self.find_elements(by=by, value=value)\n            if not elements:\n                raise NoSuchElementException(f\"Cannot locate relative element with: {by.root}\")\n            return elements[0]\n\n        return self.execute(Command.FIND_ELEMENT, {\"using\": by, \"value\": value})[\"value\"]\n\n    def find_elements(self, by: str | By | RelativeBy = By.ID, value: str | None = None) -> list[WebElement]:\n        \"\"\"Find elements given a By strategy and locator.\n\n        Args:\n            by: The locating strategy to use. Default is `By.ID`. Supported\n                values include: By.ID, By.NAME, By.XPATH, By.CSS_SELECTOR,\n                By.CLASS_NAME, By.TAG_NAME, By.LINK_TEXT, By.PARTIAL_LINK_TEXT,\n                or RelativeBy.\n            value: The locator value to use with the specified `by` strategy.\n\n        Returns:\n            List of WebElements matching locator strategy found on the page.\n\n        Example:","sourceCodeStart":889,"sourceCodeEnd":925,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/py/selenium/webdriver/remote/webdriver.py#L889-L925","documentation":"Raised by `find_element` when `by` is a `RelativeBy` (the Friendly / relative locator API) and the underlying `find_elements` returns an empty list — meaning no element matched the relative locator graph. It then raises NoSuchElementException with the root descriptor. This is a 'nothing matched' result, not a malformed locator.","triggerScenarios":"Calling driver.find_element(with_tag('button').to_right_of(el)) when no button exists to the right of the anchor; the anchor element is stale/hidden; the page hasn't rendered the target yet; filters (near/to_left_of) over-constrain.","commonSituations":"Relative locators on dynamic/async UI before targets render; anchors that scrolled out of view; overly strict multi-filter combos that exclude everything; responsive layouts where geometry differs.","solutions":["Wait for the target to be present, then retry the relative find.","Loosen the relative filters (drop a to_left_of/near constraint) to widen the match.","Switch to find_elements to handle the zero-match case gracefully, or use a direct locator as fallback."],"exampleFix":"# before\nbtn = driver.find_element(with_tag('button').to_right_of(anchor))\n\n# after\nfrom selenium.webdriver.support.ui import WebDriverWait\nbtns = WebDriverWait(driver, 10).until(\n    lambda d: d.find_elements(with_tag('button').to_right_of(anchor)))\nbtn = btns[0]","handlingStrategy":"try-catch","validationCode":"matches = driver.find_elements(by)  # RelativeBY -> [] if none\nif not matches:\n    raise NoSuchElementException('no relative match; wait or loosen filters')","typeGuard":null,"tryCatchPattern":"from selenium.common.exceptions import NoSuchElementException\ntry:\n    el = driver.find_element(rel_by)\nexcept NoSuchElementException:\n    # target not present yet; wait + retry, or fall back to a direct locator","preventionTips":["Treat relative find_element as potentially empty; prefer find_elements + wait.","Verify the anchor element is present and stable before building RelativeBY."],"tags":["locator","relative-locator","synchronization","webdriver"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}