Skip to content

[Bug]: locator.fill() can insert text into a different element if focus changes mid-call #42125

Closed as not planned

Description

Version

1.62.1

Steps to reproduce

Save the below as test.mjs and run it in Node a few times: node test.mjs

import { chromium } from "@playwright/test";

const browser = await chromium.launch();
const page = await browser.newPage();

await page.setContent(`
<input id="name"><input id="address">
<script>
  // Stands in for a focus trap / autofocus that runs asynchronously
  document.getElementById('address').addEventListener('focus', () => {
    setTimeout(() => document.getElementById('name').focus(), 0);
  });
</script>`);

await page.locator("#name").fill("Alice");
await page.locator("#address").fill("123 Main St");

console.log("name    =", await page.locator("#name").inputValue());
console.log("address =", await page.locator("#address").inputValue());

await browser.close();

Expected behavior

name = Alice
address = 123 Main St

Actual behavior

name = Alice123 Main St
address =

Additional context

According to Claude (I'm sure that's your favorite thing to hear):

For text-like inputs, locator.fill() is not atomic. It performs two steps across separate round trips:

In-page: actionability check, then injectedScript.fill(node, value), which for a text input calls selectText(element) (input.select(); input.focus();) and returns the sentinel "needsinput".
Out-of-page: page.keyboard.insertText(value) — a Input.insertText CDP call that goes to whatever element holds focus when it arrives, not to the located element.
If anything moves focus in the window between those two steps, the text is inserted into the newly focused element instead. fill() resolves successfully, so the corruption is silent — no error, no warning.

The result is doubly wrong: the target element is left empty and the unrelated element has the text appended at its caret, so a prior value is silently concatenated rather than replaced.

Environment

Chromium 151.0.7922.34 · macOS (Darwin 24.6.0) · Node v26.5.0

Activity

  1. pavelfeldman commented on Aug 4, 2026

    @pavelfeldman
    Member

    Luckily this is EXACTLY what will happen to your user, so we consider this behavior as designed.

  2. Skn0tt commented on Aug 5, 2026

    @Skn0tt
    Contributor

    Closing per above, the behaviour is by design.

  3. Loren-Johnson commented on Aug 5, 2026

    @Loren-Johnson
    Author

    Context for other users: If you use Angular Material dialogs, the default for those is to auto-focus the first element after opening animations have stopped. locator.fill doesn't wait for animations and so it is fast enough to fill one or more of the fields in the form before the auto-focus triggers and starts dumping all your text in the first box.

    We solved this by waiting for the first element to be focused before fill()ing. The only problem is you have to know if you're opening a mat-dialog or some other dialog. Bummer.

    It is very unfortunate that locator.fill() is not atomic. It already doesn't behave like a user, so I never expected it to allow this kind of flake. Very disappointing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions