MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / find_element

Method find_element

py/selenium/webdriver/remote/webdriver.py:887–911  ·  view source on GitHub ↗

Find an element given a By strategy and locator. Args: by: The locating strategy to use. Default is `By.ID`. Supported values include: By.ID, By.NAME, By.XPATH, By.CSS_SELECTOR, By.CLASS_NAME, By.TAG_NAME, By.LINK_TEXT, By.PARTIAL_LINK_TEXT,

(self, by: str | By | RelativeBy = By.ID, value: str | None = None)

Source from the content-addressed store, hash-verified

885 _ = self.execute(Command.SET_TIMEOUTS, timeouts._to_json())["value"]
886
887 def find_element(self, by: str | By | RelativeBy = By.ID, value: str | None = None) -> WebElement:
888 """Find an element given a By strategy and locator.
889
890 Args:
891 by: The locating strategy to use. Default is `By.ID`. Supported
892 values include: By.ID, By.NAME, By.XPATH, By.CSS_SELECTOR,
893 By.CLASS_NAME, By.TAG_NAME, By.LINK_TEXT, By.PARTIAL_LINK_TEXT,
894 or RelativeBy.
895 value: The locator value to use with the specified `by` strategy.
896
897 Returns:
898 The first matching WebElement found on the page.
899
900 Example:
901 `element = driver.find_element(By.ID, 'foo')`
902 """
903 by, value = self.locator_converter.convert(by, value)
904
905 if isinstance(by, RelativeBy):
906 elements = self.find_elements(by=by, value=value)
907 if not elements:
908 raise NoSuchElementException(f"Cannot locate relative element with: {by.root}")
909 return elements[0]
910
911 return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
912
913 def find_elements(self, by: str | By | RelativeBy = By.ID, value: str | None = None) -> list[WebElement]:
914 """Find elements given a By strategy and locator.

Calls 4

find_elementsMethod · 0.95
executeMethod · 0.95
convertMethod · 0.45