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

Method find_element

py/selenium/webdriver/remote/webdriver.py:886–910  ·  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

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

Calls 4

find_elementsMethod · 0.95
executeMethod · 0.95
convertMethod · 0.45