Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Call focus() on the Puppeteer Frame that contains the element: await frame.focus('#target'). For a child iframe, first locate its frame object; page.focus() only targets the page’s main frame.

Focus an element in the correct frame

A Puppeteer Frame represents a document frame, such as an <iframe>. Once you have the frame containing the target, focus the matching element:

await frame.focus('#target');

Frame.focus(selector) focuses the first matching element and throws if there is no match. See the Frame.focus API reference.

Find a child frame

Use page.frames() to inspect the page’s frame tree. To choose a frame by the name attribute of its iframe element:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const frames = page.frames();
let targetFrame;

for (const frame of frames) {
  const frameElement = await frame.frameElement();
  const name = await frameElement.evaluate(el => el.getAttribute('name'));
  if (name === 'myframe') {
    targetFrame = frame;
    break;
  }
}

if (!targetFrame) {
  throw new Error('Target frame not found');
}

await targetFrame.focus('#target');

Replace myframe and #target with the name and selector used by your page. For other selection strategies, inspect frame URLs with frame.url(), or use parentFrame() and childFrames() to navigate the frame hierarchy. A page can contain nested frames, so make sure the selected frame is the one whose document contains the element. The Frame class reference documents these methods and frame enumeration.

Use frame focus, not page focus, for an iframe

page.focus(selector) is a shortcut for page.mainFrame().focus(selector). It searches the main document, not an arbitrary child iframe. Call frame.focus(selector) on the specific child frame instead. See the Page.focus API reference.

Wait if the element renders asynchronously

If the target is added after the frame loads, wait for it in that frame before focusing:

await frame.waitForSelector('#target');
await frame.focus('#target');

Frame.waitForSelector() waits for a matching element to appear in that frame and works across navigations; it throws if the element does not appear. See the Frame.waitForSelector API reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Puppeteer’s locator guide recommends locators for selecting and interacting with elements because they wait for the element to be present and ready. The documented Locator API includes actions such as click, fill, and hover, but not a focus action; when the specific action you need is focus, use Frame.focus(). See the page interactions guide.

Choose a selector and handle missing matches

CSS selectors work by default. Puppeteer also supports selector syntax for text, accessibility attributes, XPath, and shadow DOM; choose one that identifies the intended element within the selected frame. See the selector documentation.

For a lower-level existence check, query the selected frame before focusing:

const element = await frame.$('#target');
if (!element) {
  throw new Error('Target element not found in frame');
}
await frame.focus('#target');

frame.$(selector) returns the first matching element handle or null. See the Frame class reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot focus failures

  • No matching element: Confirm the selector matches the frame’s document, not the top-level page. Use frame.$(selector) to check for a match or frame.waitForSelector(selector) if it may render later.
  • Wrong frame: Verify the selected frame by its name, URL, or place in the frame tree. A successful selector in the main frame does not establish that it exists in the child frame.
  • Frame navigated or detached: The frame may have changed while your code was locating or focusing the element. Re-enumerate page.frames(), select the current frame again, wait for the target, then focus it.
  • Page-level call misses an iframe target: Replace page.focus(selector) with frame.focus(selector) on the child frame.

Version note

Puppeteer API pages are versioned, and the Frame.focus reference at the /next/ path describes a next-version API. Check the documentation matching the Puppeteer version installed in your project when verifying version-specific behavior.

Or skip the browser setup

If your goal is to capture a website rather than automate keyboard focus, ScreenshotNeo provides a screenshot API and MCP server. One GET request can return a screenshot or PDF; this example saves a WebP screenshot:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for setup and options. It removes cookie banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, and failed loads are never billed. AI agents can take screenshots through its MCP server. The free plan includes 1,000 screenshots per month with no card, and paid plans start at $5 for 3,000.

Sign up for 1,000 free screenshots a month, with no card required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.