{"record":{"id":"a06308f89ac78c0f","repo":"SeleniumHQ/selenium","slug":"element-or-locator-must-be-given-when-calling-to-l","errorCode":null,"errorMessage":"Element or locator must be given when calling to_left_of method","messagePattern":"Element or locator must be given when calling to_left_of method","errorType":"exception","errorClass":"WebDriverException","httpStatus":null,"severity":"error","filePath":"py/selenium/webdriver/support/relative_locator.py","lineNumber":173,"sourceCode":"\n    def to_left_of(self, element_or_locator: WebElement | dict | None = None) -> \"RelativeBy\":\n        \"\"\"Add a filter to look for elements to the left of.\n\n        Args:\n            element_or_locator: Element to look to the left of\n\n        Returns:\n            RelativeBy\n\n        Raises:\n            WebDriverException: If `element_or_locator` is None.\n\n        Example:\n            >>> right = driver.find_element(By.ID, \"right\")\n            >>> elements = driver.find_elements(locate_with(By.CSS_SELECTOR, \"p\").to_left_of(right))\n        \"\"\"\n        if not element_or_locator:\n            raise WebDriverException(\"Element or locator must be given when calling to_left_of method\")\n\n        self.filters.append({\"kind\": \"left\", \"args\": [element_or_locator]})\n        return self\n\n    @overload\n    def to_right_of(self, element_or_locator: WebElement | LocatorType) -> \"RelativeBy\": ...\n\n    @overload\n    def to_right_of(self, element_or_locator: None = None) -> \"NoReturn\": ...\n\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","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/py/selenium/webdriver/support/relative_locator.py#L155-L191","documentation":"RelativeBy.to_left_of requires a non-null element_or_locator; a falsy value raises WebDriverException naming the 'to_left_of' method.","triggerScenarios":"Chaining .to_left_of(None) or passing an unresolved/empty locator.","commonSituations":"Piping a not-found anchor into .to_left_of().","solutions":["Resolve and verify the anchor before chaining .to_left_of().","Guard the value before calling.","Use find_element for anchors so misses surface early."],"exampleFix":"# before\nright = maybe_find()\nelements = driver.find_elements(locate_with(By.CSS_SELECTOR, \"p\").to_left_of(right))\n\n# after\nright = driver.find_element(By.ID, \"right\")\nelements = driver.find_elements(locate_with(By.CSS_SELECTOR, \"p\").to_left_of(right))","handlingStrategy":"type-guard","validationCode":"if not element_or_locator:\n    raise ValueError(\"anchor required before .to_left_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"}