Interface 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.frameLocator(selector) or locator.frameLocator(selector) method.

const locator = page.frameLocator('#my-frame').getByText('Submit');
await 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:
await page.frameLocator('.result-frame').getByRole('button').click();

// Works because we explicitly tell locator to pick the first frame:
await page.frameLocator('.result-frame').first().getByRole('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:

const frameLocator = locator.frameLocator(':scope');

Hierarchy

  • FrameLocator

Methods

  • Returns locator to the first matching frame.

    Returns FrameLocator

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

    Parameters

    • selector: string

      A selector to use when resolving DOM element.

    Returns FrameLocator

  • Allows locating elements by their alt text.

    Usage

    For example, this method will find the image by alt text "Playwright logo":

    <img alt='Playwright logo'>
    
    await page.getByAltText('Playwright logo').click();
    

    Parameters

    • text: string | RegExp

      Text to locate the element for.

    • Optional options: {
          exact?: boolean;
      }
      Optional
      • Optional exact?: boolean

        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> or aria-labelledby element, or by the aria-label attribute.

    Usage

    For example, this method will find inputs by label "Username" and "Password" in the following DOM:

    <input aria-label="Username">
    <label for="password-input">Password:</label>
    <input id="password-input">
    await page.getByLabel('Username').fill('john');
    await page.getByLabel('Password').fill('secret');

    Parameters

    • text: string | RegExp

      Text to locate the element for.

    • Optional options: {
          exact?: boolean;
      }
      Optional
      • Optional exact?: boolean

        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.

    Usage

    For example, consider the following DOM structure.

    <input type="email" placeholder="name@example.com" />
    

    You can fill the input after locating it by the placeholder text:

    await page
    .getByPlaceholder('name@example.com')
    .fill('playwright@microsoft.com');

    Parameters

    • text: string | RegExp

      Text to locate the element for.

    • Optional options: {
          exact?: boolean;
      }
      Optional
      • Optional exact?: boolean

        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 ARIA role, ARIA attributes and accessible name.

    Usage

    Consider the following DOM structure.

    <h3>Sign up</h3>
    <label>
    <input type="checkbox" /> Subscribe
    </label>
    <br/>
    <button>Submit</button>

    You can locate each element by it's implicit role:

    await expect(page.getByRole('heading', { name: 'Sign up' })).toBeVisible();

    await page.getByRole('checkbox', { name: 'Subscribe' }).check();

    await page.getByRole('button', { name: /submit/i }).click();

    Details

    Role selector does not replace accessibility audits and conformance tests, but rather gives early feedback about the ARIA guidelines.

    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.

    Parameters

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

      Required aria role.

    • Optional options: {
          checked?: boolean;
          disabled?: boolean;
          exact?: boolean;
          expanded?: boolean;
          includeHidden?: boolean;
          level?: number;
          name?: string | RegExp;
          pressed?: boolean;
          selected?: boolean;
      }
      Optional
      • Optional checked?: boolean

        An attribute that is usually set by aria-checked or native <input type=checkbox> controls.

        Learn more about aria-checked.

      • Optional disabled?: boolean

        An 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.

      • Optional exact?: boolean

        Whether name is matched exactly: case-sensitive and whole-string. Defaults to false. Ignored when name is a regular expression. Note that exact match still trims whitespace.

      • Optional expanded?: boolean

        An attribute that is usually set by aria-expanded.

        Learn more about aria-expanded.

      • Optional includeHidden?: boolean

        Option 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.

      • Optional level?: number

        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.

      • Optional name?: string | RegExp

        Option to match the accessible name. By default, matching is case-insensitive and searches for a substring, use exact to control this behavior.

        Learn more about accessible name.

      • Optional pressed?: boolean

        An attribute that is usually set by aria-pressed.

        Learn more about aria-pressed.

      • Optional selected?: boolean

        An attribute that is usually set by aria-selected.

        Learn more about aria-selected.

    Returns Locator

  • Locate element by the test id.

    Usage

    Consider the following DOM structure.

    <button data-testid="directions">Itinéraire</button>
    

    You can locate the element by it's test id:

    await page.getByTestId('directions').click();
    

    Details

    By default, the data-testid attribute is used as a test id. Use selectors.setTestIdAttribute(attributeName) to configure a different test id attribute if necessary.

    // Set custom test id attribute from @playwright/test config:
    import { defineConfig } from '@playwright/test';

    export default defineConfig({
    use: {
    testIdAttribute: 'data-pw'
    },
    });

    Parameters

    • testId: string | RegExp

      Id to locate the element by.

    Returns Locator

  • Allows locating elements that contain given text.

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

    Usage

    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.getByText('world');

    // Matches first <div>
    page.getByText('Hello world');

    // Matches second <div>
    page.getByText('Hello', { exact: true });

    // Matches both <div>s
    page.getByText(/Hello/);

    // Matches second <div>
    page.getByText(/^hello$/i);

    Details

    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.

    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">.

    Parameters

    • text: string | RegExp

      Text to locate the element for.

    • Optional options: {
          exact?: boolean;
      }
      Optional
      • Optional exact?: boolean

        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 attribute.

    Usage

    Consider the following DOM structure.

    <span title='Issues count'>25 issues</span>
    

    You can check the issues count after locating it by the title text:

    await expect(page.getByTitle('Issues count')).toHaveText('25 issues');
    

    Parameters

    • text: string | RegExp

      Text to locate the element for.

    • Optional options: {
          exact?: boolean;
      }
      Optional
      • Optional exact?: boolean

        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

  • Returns locator to the last matching frame.

    Returns FrameLocator

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

    Learn more about locators.

    Parameters

    • selectorOrLocator: string | Locator

      A selector or locator to use when resolving DOM element.

    • Optional options: {
          has?: Locator;
          hasNot?: Locator;
          hasNotText?: string | RegExp;
          hasText?: string | RegExp;
      }
      Optional
      • Optional has?: Locator

        Narrows down the results of the method to those which contain elements matching this relative locator. For example, article that has text=Playwright matches <article><div>Playwright</div></article>.

        Inner locator must be relative to the outer locator and is queried starting with the outer locator match, not the document root. For example, you can find content that has div in <article><content><div>Playwright</div></content></article>. However, looking for content that has article div will fail, because the inner locator must be relative and should not use any elements outside the content.

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

      • Optional hasNot?: Locator

        Matches elements that do not contain an element that matches an inner locator. Inner locator is queried against the outer one. For example, article that does not have div matches <article><span>Playwright</span></article>.

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

      • Optional hasNotText?: string | RegExp

        Matches elements that do not contain 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.

      • Optional hasText?: string | RegExp

        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

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

    Parameters

    • index: number

    Returns FrameLocator

Generated using TypeDoc