(Created on )

Puppeteer: Element is not a(n) `{value}` element

ElementHandle.toElement checks a tag name that does not match the actual node.


ElementHandle.toElement checks a tag name that does not match the actual node.

Error message

Element is not a(n) `{value}` element

{value} is replaced by the value shown in your error message.

How this error happens

toElement checks the actual tag name of a handle. It does not convert one HTML element into another.

The snippets use an open Puppeteer page named page.

await page.setContent(
    '<div id="field">Not an input</div>'
);

const field = await page.$("#field");

await field.toElement(
    "input"
);

How to fix

Select an element of the required type before narrowing the handle.

await page.setContent(
    '<input id="field">'
);

const field = await page.$("#field");
const input = await field.toElement(
    "input"
);

await input.type(
    "Ada"
);

Things to keep in mind

If the tag type is uncertain, inspect it or handle the mismatch instead of using toElement as a conversion.

All Puppeteer errors

Keep Reading

Puppeteer Guides

All Guides →