Files
sousa-gecko/testing/marionette/client/docs/advanced/findelement.md
T
Sylvestre Ledru 8f033c8241 Bug 2038819 - Convert testing/**/docs RST to Markdown r=jdescottes,firefox-source-docs-reviewers,ahal
Convert testing/{awsy,condprofile,docs,geckodriver,marionette,
mozbase,mozharness,perfdocs,performance,raptor,talos,web-platform}
RST documentation files to MyST-flavored Markdown.

Skips testing/perfdocs/generated/* (auto-generated by mach perfdocs)
and testing/web-platform/tests/* (third-party test trees).

- Move treeherder-try frontmatter substitutions under the new myst
  topmatter key.
- Allow the pre-existing autodoc Unknown target warning for
  mozprofile.permissions to keep the build clean.
- Refresh allowed-warnings glob for manifest-sandbox download from
  .rst to .md.

Differential Revision: https://phabricator.services.mozilla.com/D304228
2026-07-04 10:18:34 +00:00

3.0 KiB

Finding Elements

.. py:currentmodule:: marionette_driver.marionette

One of the most common and yet often most difficult tasks in Marionette is finding a DOM element on a webpage or in the chrome UI. Marionette provides several different search strategies to use when finding elements. All search strategies work with both {func}~Marionette.find_element and {func}~Marionette.find_elements, though some strategies are not implemented in chrome scope.

In the event that more than one element is matched by the query, {func}~Marionette.find_element will only return the first element found. In the event that no elements are matched by the query, {func}~Marionette.find_element will raise NoSuchElementException while {func}~Marionette.find_elements will return an empty list.

Search Strategies

Search strategies are defined in the {class}By class:

from marionette_driver import By
print(By.ID)

The strategies are:

  • id - The easiest way to find an element is to refer to its id directly:

    container = client.find_element(By.ID, 'container')
    
  • class name - To find elements belonging to a certain class, use class name:

    buttons = client.find_elements(By.CLASS_NAME, 'button')
    
  • css selector - It's also possible to find elements using a css selector:

    container_buttons = client.find_elements(By.CSS_SELECTOR, '#container .buttons')
    
  • name - Find elements by their name attribute (not implemented in chrome scope):

    form = client.find_element(By.NAME, 'signup')
    
  • tag name - To find all the elements with a given tag, use tag name:

    paragraphs = client.find_elements(By.TAG_NAME, 'p')
    
  • link text - A convenience strategy for finding link elements by their innerHTML (not implemented in chrome scope):

    link = client.find_element(By.LINK_TEXT, 'Click me!')
    
  • partial link text - Same as link text except substrings of the innerHTML are matched (not implemented in chrome scope):

    link = client.find_element(By.PARTIAL_LINK_TEXT, 'Clic')
    
  • xpath - Find elements using an xpath query:

    elem = client.find_element(By.XPATH, './/*[@id="foobar"')
    

Chaining Searches

In addition to the methods on the Marionette object, WebElement objects also provide {func}~WebElement.find_element and {func}~WebElement.find_elements methods. The difference is that only child nodes of the element will be searched. Consider the following html snippet:

<div id="content">
    <span id="main"></span>
</div>
<div id="footer"></div>

Doing the following will work:

client.find_element(By.ID, 'container').find_element(By.ID, 'main')

But this will raise a NoSuchElementException:

client.find_element(By.ID, 'container').find_element(By.ID, 'footer')