Find elements 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. - B
(self, by: str | By | RelativeBy = By.ID, value: str | None = None)
| 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. |
| 102 | |
| 103 | Args: |
| 104 | by: The locating strategy to use. Default is `By.ID`. Supported values include: |
| 105 | - By.ID: Locate by element ID. |
| 106 | - By.NAME: Locate by the `name` attribute. |
| 107 | - By.XPATH: Locate by an XPath expression. |
| 108 | - By.CSS_SELECTOR: Locate by a CSS selector. |
| 109 | - By.CLASS_NAME: Locate by the `class` attribute. |
| 110 | - By.TAG_NAME: Locate by the tag name (e.g., "input", "button"). |
| 111 | - By.LINK_TEXT: Locate a link element by its exact text. |
| 112 | - By.PARTIAL_LINK_TEXT: Locate a link element by partial text match. |
| 113 | value: The locator value to use with the specified `by` strategy. |
| 114 | |
| 115 | Returns: |
| 116 | List of `WebElements` matching locator strategy found on the page. |
| 117 | |
| 118 | Example: |
| 119 | >>> element = driver.find_elements(By.ID, "foo") |
| 120 | """ |
| 121 | from selenium.webdriver.support.relative_locator import RelativeBy |
| 122 | |
| 123 | if isinstance(by, RelativeBy): |
| 124 | return self.session._find_elements_relative(by, root=self) |
| 125 | |
| 126 | if by == By.ID: |
| 127 | by = By.CSS_SELECTOR |
| 128 | value = f'[id="{value}"]' |
| 129 | elif by == By.CLASS_NAME: |
| 130 | if value and any(char.isspace() for char in value.strip()): |
| 131 | raise InvalidSelectorException("Compound class names are not allowed.") |
| 132 | by = By.CSS_SELECTOR |
| 133 | value = f".{value}" |
| 134 | elif by == By.NAME: |
| 135 | by = By.CSS_SELECTOR |
| 136 | value = f'[name="{value}"]' |
| 137 | |
| 138 | return self._execute(Command.FIND_ELEMENTS_FROM_SHADOW_ROOT, {"using": by, "value": value})["value"] |
| 139 | |
| 140 | # Private Methods |
| 141 | def _execute(self, command, params=None): |
no test coverage detected