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

Method find_element

py/selenium/webdriver/remote/webelement.py:534–566  ·  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: Locate by element ID. - By.NAME: Locate by the `name` attribute. - By.XPATH: Locate by

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

Source from the content-addressed store, hash-verified

532 return self._parent.execute(command, params)
533
534 def find_element(self, by: str | By | RelativeBy = By.ID, value: str | None = None) -> WebElement:
535 """Find an element given a By strategy and locator.
536
537 Args:
538 by: The locating strategy to use. Default is `By.ID`. Supported values include:
539 - By.ID: Locate by element ID.
540 - By.NAME: Locate by the `name` attribute.
541 - By.XPATH: Locate by an XPath expression.
542 - By.CSS_SELECTOR: Locate by a CSS selector.
543 - By.CLASS_NAME: Locate by the `class` attribute.
544 - By.TAG_NAME: Locate by the tag name (e.g., "input", "button").
545 - By.LINK_TEXT: Locate a link element by its exact text.
546 - By.PARTIAL_LINK_TEXT: Locate a link element by partial text match.
547 value: The locator value to use with the specified `by` strategy.
548
549 Returns:
550 The first matching `WebElement` found on the page.
551
552 Example:
553 element = driver.find_element(By.ID, "foo")
554 """
555 # imported here because relative_locator imports this module
556 from selenium.webdriver.support.relative_locator import RelativeBy
557
558 by, value = self._parent.locator_converter.convert(by, value)
559
560 if isinstance(by, RelativeBy):
561 elements = self.find_elements(by=by, value=value)
562 if not elements:
563 raise NoSuchElementException(f"Cannot locate relative element with: {by.root}")
564 return elements[0]
565
566 return self._execute(Command.FIND_CHILD_ELEMENT, {"using": by, "value": value})["value"]
567
568 def find_elements(self, by: str | By | RelativeBy = By.ID, value: str | None = None) -> list[WebElement]:
569 """Find elements given a By strategy and locator.

Callers

nothing calls this directly

Calls 4

find_elementsMethod · 0.95
_executeMethod · 0.95
convertMethod · 0.45

Tested by

no test coverage detected