Find an element inside a shadow root given a By strategy and locator. Args: by: The locating strategy to use. Default is `By.ID`. Supported values include: - By.ID: Locate by element ID. - By.NAME: Locate by the `name` attribute. -
(self, by: str | By | RelativeBy = By.ID, value: str | None = None)
| 54 | return self._id |
| 55 | |
| 56 | def find_element(self, by: str | By | RelativeBy = By.ID, value: str | None = None) -> WebElement: |
| 57 | """Find an element inside a shadow root given a By strategy and locator. |
| 58 | |
| 59 | Args: |
| 60 | by: The locating strategy to use. Default is `By.ID`. Supported values include: |
| 61 | - By.ID: Locate by element ID. |
| 62 | - By.NAME: Locate by the `name` attribute. |
| 63 | - By.XPATH: Locate by an XPath expression. |
| 64 | - By.CSS_SELECTOR: Locate by a CSS selector. |
| 65 | - By.CLASS_NAME: Locate by the `class` attribute. |
| 66 | - By.TAG_NAME: Locate by the tag name (e.g., "input", "button"). |
| 67 | - By.LINK_TEXT: Locate a link element by its exact text. |
| 68 | - By.PARTIAL_LINK_TEXT: Locate a link element by partial text match. |
| 69 | value: The locator value to use with the specified `by` strategy. |
| 70 | |
| 71 | Returns: |
| 72 | The first matching `WebElement` found on the page. |
| 73 | |
| 74 | Example: |
| 75 | >>> element = driver.find_element(By.ID, "foo") |
| 76 | """ |
| 77 | # imported here because relative_locator imports webelement, which imports this module |
| 78 | from selenium.webdriver.support.relative_locator import RelativeBy |
| 79 | |
| 80 | if isinstance(by, RelativeBy): |
| 81 | elements = self.find_elements(by) |
| 82 | if not elements: |
| 83 | raise NoSuchElementException(f"Cannot locate relative element with: {by.root}") |
| 84 | return elements[0] |
| 85 | |
| 86 | if by == By.ID: |
| 87 | by = By.CSS_SELECTOR |
| 88 | value = f'[id="{value}"]' |
| 89 | elif by == By.CLASS_NAME: |
| 90 | if value and any(char.isspace() for char in value.strip()): |
| 91 | raise InvalidSelectorException("Compound class names are not allowed.") |
| 92 | by = By.CSS_SELECTOR |
| 93 | value = f".{value}" |
| 94 | elif by == By.NAME: |
| 95 | by = By.CSS_SELECTOR |
| 96 | value = f'[name="{value}"]' |
| 97 | |
| 98 | return self._execute(Command.FIND_ELEMENT_FROM_SHADOW_ROOT, {"using": by, "value": value})["value"] |
| 99 | |
| 100 | def find_elements(self, by: str | By | RelativeBy = By.ID, value: str | None = None) -> list[WebElement]: |
| 101 | """Find elements inside a shadow root given a By strategy and locator. |
nothing calls this directly
no test coverage detected