Skip to main content

FrameLocator

FrameLocator represents a view to the iframe on the page. It captures the logic sufficient to retrieve the iframe and locate elements in that iframe. FrameLocator can be created with either page.frame_locator(selector) or locator.frame_locator(selector) method.

locator = page.frame_locator("my-frame").get_by_text("Submit")
locator.click()

Strictness

Frame locators are strict. This means that all operations on frame locators will throw if more than one element matches a given selector.

# Throws if there are several frames in DOM:
page.frame_locator('.result-frame').get_by_role('button').click()

# Works because we explicitly tell locator to pick the first frame:
page.frame_locator('.result-frame').first.get_by_role('button').click()

Converting Locator to FrameLocator

If you have a Locator object pointing to an iframe it can be converted to FrameLocator using :scope CSS selector:

frameLocator = locator.frame_locator(":scope");

frame_locator.first

Added in: v1.17

Returns locator to the first matching frame.

frame_locator.frame_locator(selector)

Added in: v1.17

When working with iframes, you can create a frame locator that will enter the iframe and allow selecting elements in that iframe.

frame_locator.get_by_alt_text(text, **kwargs)

Added in: v1.27
  • text <str|Pattern> Text to locate the element for.#
  • exact <bool> Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular expression. Note that exact match still trims whitespace.#
  • returns: <Locator>#

Allows locating elements by their alt text. For example, this method will find the image by alt text "Castle":

<img alt='Castle'>

frame_locator.get_by_label(text, **kwargs)

Added in: v1.27
  • text <str|Pattern> Text to locate the element for.#
  • exact <bool> Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular expression. Note that exact match still trims whitespace.#
  • returns: <Locator>#

Allows locating input elements by the text of the associated label. For example, this method will find the input by label text Password in the following DOM:

<label for="password-input">Password:</label>
<input id="password-input">

frame_locator.get_by_placeholder(text, **kwargs)

Added in: v1.27
  • text <str|Pattern> Text to locate the element for.#
  • exact <bool> Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular expression. Note that exact match still trims whitespace.#
  • returns: <Locator>#

Allows locating input elements by the placeholder text. For example, this method will find the input by placeholder "Country":

<input placeholder="Country">

frame_locator.get_by_role(role, **kwargs)

Added in: v1.27
  • role <"alert"|"alertdialog"|"application"|"article"|"banner"|"blockquote"|"button"|"caption"|"cell"|"checkbox"|"code"|"columnheader"|"combobox"|"complementary"|"contentinfo"|"definition"|"deletion"|"dialog"|"directory"|"document"|"emphasis"|"feed"|"figure"|"form"|"generic"|"grid"|"gridcell"|"group"|"heading"|"img"|"insertion"|"link"|"list"|"listbox"|"listitem"|"log"|"main"|"marquee"|"math"|"meter"|"menu"|"menubar"|"menuitem"|"menuitemcheckbox"|"menuitemradio"|"navigation"|"none"|"note"|"option"|"paragraph"|"presentation"|"progressbar"|"radio"|"radiogroup"|"region"|"row"|"rowgroup"|"rowheader"|"scrollbar"|"search"|"searchbox"|"separator"|"slider"|"spinbutton"|"status"|"strong"|"subscript"|"superscript"|"switch"|"tab"|"table"|"tablist"|"tabpanel"|"term"|"textbox"|"time"|"timer"|"toolbar"|"tooltip"|"tree"|"treegrid"|"treeitem"> Required aria role.#

  • checked <bool> An attribute that is usually set by aria-checked or native <input type=checkbox> controls. Available values for checked are true, false and "mixed".#

    Learn more about aria-checked.

  • disabled <bool> A boolean attribute that is usually set by aria-disabled or disabled.#

    note

    Unlike most other attributes, disabled is inherited through the DOM hierarchy. Learn more about aria-disabled.

  • expanded <bool> A boolean attribute that is usually set by aria-expanded.#

    Learn more about aria-expanded.

  • include_hidden <bool> A boolean attribute that controls whether hidden elements are matched. By default, only non-hidden elements, as defined by ARIA, are matched by role selector.#

    Learn more about aria-hidden.

  • level <int> A number attribute that is usually present for roles heading, listitem, row, treeitem, with default values for <h1>-<h6> elements.#

    Learn more about aria-level.

  • name <str|Pattern> A string attribute that matches accessible name.#

    Learn more about accessible name.

  • pressed <bool> An attribute that is usually set by aria-pressed. Available values for pressed are true, false and "mixed".#

    Learn more about aria-pressed.

  • selected <bool> A boolean attribute that is usually set by aria-selected.#

    Learn more about aria-selected.

  • returns: <Locator>#

Allows locating elements by their ARIA role, ARIA attributes and accessible name. Note that role selector does not replace accessibility audits and conformance tests, but rather gives early feedback about the ARIA guidelines.

Note that many html elements have an implicitly defined role that is recognized by the role selector. You can find all the supported roles here. ARIA guidelines do not recommend duplicating implicit roles and attributes by setting role and/or aria-* attributes to default values.

frame_locator.get_by_test_id(test_id)

Added in: v1.27
  • test_id <str> Id to locate the element by.#
  • returns: <Locator>#

Locate element by the test id. By default, the data-testid attribute is used as a test id. Use selectors.set_test_id_attribute(attribute_name) to configure a different test id attribute if necessary.

frame_locator.get_by_text(text, **kwargs)

Added in: v1.27
  • text <str|Pattern> Text to locate the element for.#
  • exact <bool> Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular expression. Note that exact match still trims whitespace.#
  • returns: <Locator>#

Allows locating elements that contain given text. Consider the following DOM structure:

<div>Hello <span>world</span></div>
<div>Hello</div>

You can locate by text substring, exact string, or a regular expression:

# Matches <span>
page.get_by_text("world")

# Matches first <div>
page.get_by_text("Hello world")

# Matches second <div>
page.get_by_text("Hello", exact=True)

# Matches both <div>s
page.get_by_text(re.compile("Hello"))

# Matches second <div>
page.get_by_text(re.compile("^hello$", re.IGNORECASE))

See also locator.filter(**kwargs) that allows to match by another criteria, like an accessible role, and then filter by the text content.

note

Matching by text always normalizes whitespace, even with exact match. For example, it turns multiple spaces into one, turns line breaks into spaces and ignores leading and trailing whitespace.

note

Input elements of the type button and submit are matched by their value instead of the text content. For example, locating by text "Log in" matches <input type=button value="Log in">.

frame_locator.get_by_title(text, **kwargs)

Added in: v1.27
  • text <str|Pattern> Text to locate the element for.#
  • exact <bool> Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular expression. Note that exact match still trims whitespace.#
  • returns: <Locator>#

Allows locating elements by their title. For example, this method will find the button by its title "Submit":

<button title='Place the order'>Order Now</button>

frame_locator.last

Added in: v1.17

Returns locator to the last matching frame.

frame_locator.locator(selector, **kwargs)

Added in: v1.17
  • selector <str> A selector to use when resolving DOM element. See working with selectors for more details.#

  • has <Locator> Matches elements containing an element that matches an inner locator. Inner locator is queried against the outer one. For example, article that has text=Playwright matches <article><div>Playwright</div></article>.#

    Note that outer and inner locators must belong to the same frame. Inner locator must not contain FrameLocators.

  • has_text <str|Pattern> Matches elements containing specified text somewhere inside, possibly in a child or a descendant element. When passed a [string], matching is case-insensitive and searches for a substring. For example, "Playwright" matches <article><div>Playwright</div></article>.#

  • returns: <Locator>#

The method finds an element matching the specified selector in the locator's subtree. It also accepts filter options, similar to locator.filter(**kwargs) method.

Learn more about locators.

frame_locator.nth(index)

Added in: v1.17

Returns locator to the n-th matching frame. It's zero based, nth(0) selects the first frame.