{"record":{"id":"ba5a141ed2854e9f","repo":"SeleniumHQ/selenium","slug":"distance-must-be-positive","errorCode":null,"errorMessage":"Distance must be positive","messagePattern":"Distance must be positive","errorType":"exception","errorClass":"WebDriverException","httpStatus":null,"severity":"error","filePath":"py/selenium/webdriver/support/relative_locator.py","lineNumber":305,"sourceCode":"        Args:\n            element_or_locator: Element to look near by the element or within a distance\n            distance: Distance in pixel\n\n        Returns:\n            RelativeBy\n\n        Raises:\n            WebDriverException: If `element_or_locator` is None\n            WebDriverException: If `distance` is less than or equal to 0.\n\n        Example:\n            >>> near = driver.find_element(By.ID, \"near\")\n            >>> elements = driver.find_elements(locate_with(By.CSS_SELECTOR, \"p\").near(near, 50))\n        \"\"\"\n        if not element_or_locator:\n            raise WebDriverException(\"Element or locator must be given when calling near method\")\n        if distance <= 0:\n            raise WebDriverException(\"Distance must be positive\")\n\n        self.filters.append({\"kind\": \"near\", \"args\": [element_or_locator, distance]})\n        return self\n\n    def to_dict(self) -> dict:\n        \"\"\"Create a dict to be passed to the driver for element searching.\"\"\"\n        return {\n            \"relative\": {\n                \"root\": self.root,\n                \"filters\": self.filters,\n            }\n        }\n","sourceCodeStart":287,"sourceCodeEnd":318,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/py/selenium/webdriver/support/relative_locator.py#L287-L318","documentation":"`RelativeBy.near()` validates that its `distance` argument (pixels, default 50) is strictly positive. A value of 0 or negative raises WebDriverException because a non-positive radius would never match any neighbouring element.","triggerScenarios":"Calling `locate_with(...).near(anchor, distance=0)`, a negative distance, or passing a computed distance that evaluated to <= 0 (e.g. from a layout measurement that returned 0).","commonSituations":"Passing 0 to mean 'as close as possible'; deriving distance from a possibly-zero element rect calculation; off-by-one when converting units.","solutions":["Pass a positive integer: `locator.near(anchor, distance=50)`","If distance is computed, clamp it to a positive minimum: `distance = max(distance, 1)`","Omit the argument entirely to use the default of 50 pixels"],"exampleFix":"// before\nloc = locate_with(By.CSS_SELECTOR, \"p\").near(near, 0)\n\n// after\nloc = locate_with(By.CSS_SELECTOR, \"p\").near(near, 50)","handlingStrategy":"validation","validationCode":"distance = max(distance, 1)  # clamp to positive\nloc = locate_with(By.CSS_SELECTOR, \"p\").near(anchor, distance)","typeGuard":"def is_positive_distance(d) -> bool:\n    return isinstance(d, (int, float)) and d > 0","tryCatchPattern":null,"preventionTips":["Omit distance to use the safe default of 50","Clamp computed distances to a positive minimum before passing","Never pass 0 or negative distances intending 'closest'"],"tags":["relative-locator","validation","selenium-python"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}