You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: locator.fill() can insert text into a different element if focus changes mid-call #42125
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.
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.
Activity
pavelfeldman commentedon Aug 4, 2026
Luckily this is EXACTLY what will happen to your user, so we consider this behavior as designed.
Skn0tt commentedon Aug 5, 2026
Closing per above, the behaviour is by design.
Loren-Johnson commentedon Aug 5, 2026
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.filldoesn'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.