{"record":{"id":"c7f84198d389f18c","repo":"SeleniumHQ/selenium","slug":"element-or-locator-must-be-given-when-calling-abov","errorCode":null,"errorMessage":"Element or locator must be given when calling above method","messagePattern":"Element or locator must be given when calling above method","errorType":"exception","errorClass":"WebDriverException","httpStatus":null,"severity":"error","filePath":"py/selenium/webdriver/support/relative_locator.py","lineNumber":117,"sourceCode":"    def 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        Returns:\n            RelativeBy\n\n        Raises:\n            WebDriverException: If `element_or_locator` is None.\n\n        Example:\n        --------\n        >>> lowest = driver.find_element(By.ID, \"below\")\n        >>> elements = driver.find_elements(locate_with(By.CSS_SELECTOR, \"p\").above(lowest))\n        \"\"\"\n        if not element_or_locator:\n            raise WebDriverException(\"Element or locator must be given when calling above method\")\n\n        self.filters.append({\"kind\": \"above\", \"args\": [element_or_locator]})\n        return self\n\n    @overload\n    def below(self, element_or_locator: WebElement | LocatorType) -> \"RelativeBy\": ...\n\n    @overload\n    def below(self, element_or_locator: None = None) -> \"NoReturn\": ...\n\n    def below(self, element_or_locator: WebElement | dict | None = None) -> \"RelativeBy\":\n        \"\"\"Add a filter to look for elements below.\n\n        Args:\n            element_or_locator: Element to look below\n\n        Returns:\n            RelativeBy","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/py/selenium/webdriver/support/relative_locator.py#L99-L135","documentation":"RelativeBy.above requires a non-null element_or_locator (a WebElement or a locator dict). Passing a falsy value (None, empty dict, 0) raises WebDriverException naming the 'above' method.","triggerScenarios":"Chaining .above(None), or passing a variable holding a lookup result that resolved to None (e.g. find_element returned nothing usable).","commonSituations":"Resolving an anchor element that was not found and piping it straight into .above().","solutions":["Resolve and verify the anchor element/locator before chaining .above().","Guard the value with a truthiness/None check before calling.","Use find_element (which raises on miss) rather than optional lookups for anchors."],"exampleFix":"# before\nanchor = maybe_find()           # may be None\nelements = driver.find_elements(locate_with(By.CSS_SELECTOR, \"p\").above(anchor))\n\n# after\nanchor = driver.find_element(By.ID, \"anchor\")  # raises if absent\nelements = driver.find_elements(locate_with(By.CSS_SELECTOR, \"p\").above(anchor))","handlingStrategy":"type-guard","validationCode":"if not element_or_locator:\n    raise ValueError(\"anchor required before .above()\")","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":["Always assert the anchor is non-None before chaining a direction method.","Prefer find_element over optional lookups for anchor elements."],"tags":["relative-locator","validation"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}