(Created on )

Puppeteer: Cannot focus non-HTMLElement

focus is called on a node that is not an HTMLElement.


focus is called on a node that is not an HTMLElement.

Error message

Cannot focus non-HTMLElement

How this error happens

An SVG element is not an HTMLElement. Calling ElementHandle.focus on an SVG node fails the API’s element-type check.

The snippets use an open Puppeteer page named page.

await page.setContent(
    '<svg id="icon" width="100" height="100"></svg>'
);

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

await icon.focus();

How to fix

Focus the HTML control that owns the interaction, not its decorative SVG child.

await page.setContent(
    '<button id="action"><svg width="20" height="20"></svg>Save</button>'
);

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

await action.focus();

Things to keep in mind

If an SVG itself must receive focus, test its accessibility and browser-specific focus behavior instead of assuming the HTML helper supports it.

All Puppeteer errors

Keep Reading

Puppeteer Guides

All Guides →