日本語

Playwright fill() error “Element is not an ” and how to fix it

​​Playwright​E2E Testing,Troubleshooting,TypeScript

Playwright's fill() fails with an error that starts with Element is not an <input> when the locator points to a wrapper element, such as a div, instead of the input itself. This happens in E2E tests for login screens and contact forms when getByTestId() is used on a component whose test ID is on an outer div. If you point the locator at an <input>, <textarea>, or contenteditable element, the error goes away.

I reproduced this error in my test environment and included the exact message it printed. I also list the errors you get when calling fill() on a select or a checkbox, which have different wording.

Note

My test environment was Ubuntu 24.04, Node.js 22.23.5, and @playwright/test 1.63.0. I wrote the tests in TypeScript and ran them only in headless Chromium. I did not try Firefox, WebKit, or Playwright in other languages such as Python. The test pages were built on the spot with page.setContent(), not taken from an external site.

The error you get when fill() targets a div that wraps an input

For the test, I used a form where a label and an input are wrapped in a div, and that div has data-testid="email".

html
<form>
  <div class="text-field" data-testid="email">
    <label for="email">メールアドレス</label>
    <input id="email" type="email">
  </div>
</form>

I wrote a test that uses this data-testid to enter text:

ts
test('入力欄を包んだ div に fill する', async ({ page }) => {
  await page.setContent(wrappedPage);
  await page.getByTestId('email').fill('taro@example.com');
  await expect(page.locator('#email')).toHaveValue('taro@example.com');
});

When I ran it, it failed with the following error. I replaced the path with /path/to/app.

text
  1) tests/bad.spec.ts:4:1 › 入力欄を包んだ div に fill する ─────────────────────────────────────────────────

    Error: locator.fill: Error: Element is not an <input>, <textarea>, <select> or [contenteditable] and does not have a role allowing [aria-readonly]
    Call log:
      - waiting for getByTestId('email')
        - locator resolved to <div class="text-field" data-testid="email">…</div>
        - fill("taro@example.com")
      - attempting fill action
        - waiting for element to be visible, enabled and editable


      4 | test('入力欄を包んだ div に fill する', async ({ page }) => {
      5 |   await page.setContent(wrappedPage);
    > 6 |   await page.getByTestId('email').fill('taro@example.com');
        |                                   ^
      7 |   await expect(page.locator('#email')).toHaveValue('taro@example.com');
      8 | });
      9 |
        at /path/to/app/tests/bad.spec.ts:6:35

It failed after 147 ms, without waiting for the test timeout (set to 10 seconds).

Check the targeted element in the resolved to line of the call log

The cause is shown in the call log line locator resolved to <div class="text-field" data-testid="email">…</div>. fill() tried to type into the div, not into the input inside it.

In the image below, the dashed frame marks the div that getByTestId('email') matched. The input where the text should go is the one inside it, marked by the solid frame.

A screen showing a div that contains an email address label and an input field. The div is outlined with a dashed frame and the input with a solid frame. The labels in the image are in Japanese. The dashed frame is labeled as the div that getByTestId('email') points to (fill() fails on it), and the solid frame is labeled as the input to fill with fill().
The div matched by getByTestId(’email’) (dashed frame) and the input inside it (solid frame)

The fill() reference page says that it throws an error when the target is not an <input>, <textarea>, or [contenteditable] element. Even when an input is inside the div, fill() does not look for it.

Point the locator at the input

The fix is to make the locator point to the input. I tried two ways, and both passed.

If the label is linked to the input, getByLabel() finds the input directly and is the shortest option.

ts
test('ラベルから input を探して fill する', async ({ page }) => {
  await page.setContent(wrappedPage);
  await page.getByLabel('メールアドレス').fill('taro@example.com');
  await expect(page.locator('#email')).toHaveValue('taro@example.com');
});

If you want to keep using data-testid, chain getByRole('textbox') after the locator for the div to narrow it down to the input inside.

ts
test('div の中の textbox に絞ってから fill する', async ({ page }) => {
  await page.setContent(wrappedPage);
  await page.getByTestId('email').getByRole('textbox').fill('taro@example.com');
  await expect(page.locator('#email')).toHaveValue('taro@example.com');
});

A label element is an exception. When I ran fill() on a locator that pointed to a label, the text went into the input linked by for, and the test passed. The reference page also says that when the target is inside a <label> that has an associated input, the input receives the text.

ts
test('label 要素に fill すると、結びついた input に入る', async ({ page }) => {
  await page.setContent(wrappedPage);
  await page.locator('label[for="email"]').fill('taro@example.com');
  await expect(page.locator('#email')).toHaveValue('taro@example.com');
});

For rich text inputs, target the contenteditable element

Rich text editors, which combine a toolbar of buttons and a body input in one component, also fail with the same error when fill() targets the outer element. In my test, the outer div had data-testid, and the inner div was contenteditable.

html
<div class="editor" data-testid="body">
  <div class="toolbar"><button type="button">太字</button></div>
  <div class="content" contenteditable="true" aria-label="本文" style="min-height: 4em"></div>
</div>

When I called fill() on the outer element, the call log showed locator resolved to <div class="editor" data-testid="body">…</div>, and the test stopped with the same error message as before.

text
    Error: locator.fill: Error: Element is not an <input>, <textarea>, <select> or [contenteditable] and does not have a role allowing [aria-readonly]
    Call log:
      - waiting for getByTestId('body')
        - locator resolved to <div class="editor" data-testid="body">…</div>
        - fill("こんにちは")

fill() can enter text into a contenteditable element. When I narrowed the outer locator down to [contenteditable], the test passed and the Japanese greeting "konnichiwa" appeared in the body.

ts
test('contenteditable の要素に fill する', async ({ page }) => {
  await page.setContent(editorPage);
  await page.getByTestId('body').locator('[contenteditable]').fill('こんにちは');
  await expect(page.locator('.content')).toHaveText('こんにちは');
});

Use methods other than fill() for select and checkboxes

Calling fill() on a select or a checkbox produces different error messages. In both cases, the locator points to the correct element, so the fix is to use a different method.

When I called fill('東京都') on a select, the error message was different from the first one and did not mention <select>.

text
    Error: locator.fill: Error: Element is not an <input>, <textarea> or [contenteditable] element
    Call log:
      - waiting for getByLabel('都道府県')
        - locator resolved to <select id="pref">…</select>
        - fill("東京都")

To choose an option in a select, use selectOption(). The string you pass is matched against both the option's value and its visible text. When I passed the Japanese word for Tokyo, the option whose value was tokyo was selected.

ts
test('select は selectOption で選ぶ', async ({ page }) => {
  await page.setContent(selectPage);
  await page.getByLabel('都道府県').selectOption('東京都');
  await expect(page.getByLabel('都道府県')).toHaveValue('tokyo');
});

A checkbox is an input element, but fill() cannot be used on it. The error message includes the input's type.

text
    Error: locator.fill: Error: Input of type "checkbox" cannot be filled
    Call log:
      - waiting for getByLabel('利用規約に同意する')
        - locator resolved to <input type="checkbox"/>
        - fill("on")

To check a checkbox, use check().

ts
test('チェックボックスは check で入れる', async ({ page }) => {
  await page.setContent('<label><input type="checkbox"> 利用規約に同意する</label>');
  await page.getByLabel('利用規約に同意する').check();
  await expect(page.getByLabel('利用規約に同意する')).toBeChecked();
});

All four failing cases stopped after about 120 to 150 ms. The six fixed cases passed in 1.2 seconds in total, and the errors no longer appeared.

Error message Element the locator pointed to Fix
Element is not an <input>, <textarea>, <select> or [contenteditable] and does not have a role allowing [aria-readonly] A div that wraps the input Point the locator at the input, for example with getByLabel()
Element is not an <input>, <textarea> or [contenteditable] element select Use selectOption()
Input of type "checkbox" cannot be filled Checkbox Use check()

​Posted by 86