{"record":{"id":"8d614776630b01a2","repo":"SeleniumHQ/selenium","slug":"element-or-locator-must-be-given-when-calling-to-r","errorCode":null,"errorMessage":"Element or locator must be given when calling to_right_of method","messagePattern":"Element or locator must be given when calling to_right_of method","errorType":"exception","errorClass":"WebDriverException","httpStatus":null,"severity":"error","filePath":"py/selenium/webdriver/support/relative_locator.py","lineNumber":201,"sourceCode":"\n    def to_right_of(self, element_or_locator: WebElement | dict | None = None) -> \"RelativeBy\":\n        \"\"\"Add a filter to look for elements right of.\n\n        Args:\n            element_or_locator: Element to look right of\n\n        Returns:\n            RelativeBy\n\n        Raises:\n            WebDriverException: If `element_or_locator` is None.\n\n        Example:\n            >>> left = driver.find_element(By.ID, \"left\")\n            >>> elements = driver.find_elements(locate_with(By.CSS_SELECTOR, \"p\").to_right_of(left))\n        \"\"\"\n        if not element_or_locator:\n            raise WebDriverException(\"Element or locator must be given when calling to_right_of method\")\n\n        self.filters.append({\"kind\": \"right\", \"args\": [element_or_locator]})\n        return self\n\n    @overload\n    def straight_above(self, element_or_locator: WebElement | LocatorType) -> \"RelativeBy\": ...\n\n    @overload\n    def straight_above(self, element_or_locator: None = None) -> \"NoReturn\": ...\n\n    def straight_above(self, element_or_locator: WebElement | LocatorType | None = None) -> \"RelativeBy\":\n        \"\"\"Add a filter to look for elements above.\n\n        Args:\n            element_or_locator: Element to look above\n        \"\"\"\n        if not element_or_locator:\n            raise WebDriverException(\"Element or locator must be given when calling above method\")","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/py/selenium/webdriver/support/relative_locator.py#L183-L219","documentation":"RelativeBy.to_right_of requires a non-null element_or_locator; a falsy value raises WebDriverException naming the 'to_right_of' method.","triggerScenarios":"Chaining .to_right_of(None) or passing an unresolved/empty locator.","commonSituations":"Piping a not-found anchor into .to_right_of().","solutions":["Resolve and verify the anchor before chaining .to_right_of().","Guard the value before calling.","Use find_element for anchors so misses surface early."],"exampleFix":"# before\nleft = maybe_find()\nelements = driver.find_elements(locate_with(By.CSS_SELECTOR, \"p\").to_right_of(left))\n\n# after\nleft = driver.find_element(By.ID, \"left\")\nelements = driver.find_elements(locate_with(By.CSS_SELECTOR, \"p\").to_right_of(left))","handlingStrategy":"type-guard","validationCode":"if not element_or_locator:\n    raise ValueError(\"anchor required before .to_right_of()\")","typeGuard":"from selenium.webdriver.remote.webelement import WebElement\ndef is_anchor(v) -> bool:\n    return isinstance(v, WebElement) or (isinstance(v, dict) and v)","tryCatchPattern":null,"preventionTips":["Assert the anchor is non-None before chaining direction methods.","Resolve anchors with find_element, not optional lookups."],"tags":["relative-locator","validation"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}