October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Focus an Element Inside a Frame with Puppeteer

Focus an element inside an iframe by selecting its Puppeteer Frame and calling frame.focus(selector).

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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'). page.focus() targets only the main frame, so it will not focus an element inside a child iframe.

Focus the element in the frame that contains it

A Puppeteer Frame represents a document frame, such as an <iframe>. Once you have the right frame object, call frame.focus(selector):

As an Amazon Associate I earn from qualifying purchases.

await frame.focus('#target');

The method focuses the first element matching the selector in that frame. It throws if no element matches, so make sure the selector identifies the intended element in that frame’s document.

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

Find the right frame

Use page.frames() to inspect the current frame tree. To select a child frame by its iframe element’s name, inspect each child frame’s frame element and read its name:

const frames = page.frames();
let targetFrame;

for (const frame of frames) {
  if (frame === page.mainFrame()) continue;

  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');

For other selection strategies, the frame API exposes url(), parentFrame() and childFrames(). Adapt the condition to a stable property of the page; nested frames may require inspecting the full hierarchy.

Wait when the target renders asynchronously

If the frame is available but its target element appears later, wait within that frame before focusing:

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

Frame.waitForSelector() waits for a matching element in that frame and works across navigations. It throws if the element does not appear. Puppeteer also offers locators for interactions that wait until an element is present and ready, but the documented locator actions do not include focus; use Frame.focus() when focus itself is the required action.

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

Choose the selector and handle failures

CSS selectors work by default. Puppeteer also documents selector syntax for text, accessibility attributes, XPath and shadow DOM. Choose a selector that matches the intended element inside the selected frame, not merely somewhere on the page.

  • No matching element: Frame.focus() throws when its selector has no match. Check that the selector is valid for the frame’s document and wait if rendering is delayed.
  • Frame not found: Check the frame-selection condition and whether the iframe is nested or identified by a different property.
  • Frame navigated or detached: Reinspect page.frames() and select the current frame before querying or focusing.

For a lower-level check, frame.$(selector) returns the first matching element handle or null:

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

Or skip the browser setup

If your goal is a screenshot rather than controlling focus in Puppeteer, ScreenshotNeo provides a screenshot API. A single request can capture a URL without setting up a browser script:

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 request options. Cookie banners, newsletter popups and chat widgets are removed before capture; bot checks, blank pages and failed loads are not billed. Its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card, and paid plans start at $5 for 3,000. Sign up for the free plan.

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

Version note

Puppeteer’s API references are versioned, and the available references include 25.9.0, 25.10.0 and 25.12.0. The /next/ API path describes a next version rather than a released-version guarantee; check the Frame API for the Puppeteer version installed in your project when relying on version-specific behavior.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.