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

To capture one part of a web page, first identify it with a locator, then use an element-level screenshot method. In Playwright, for example, a locator can target a button, heading, image, or other element; a page screenshot with fullPage: true captures the scrollable page instead. If you need the element’s markup rather than an image, extract its HTML separately.

Choose what you want to capture

“Target an HTML element” can mean finding an element for a screenshot, capturing the whole page, or extracting markup. These are separate operations:

  • Element screenshot: capture the rendered pixels of one identified element.
  • Full-page screenshot: capture the page rather than a single element. In Playwright, use the Page screenshot API with its full-page option.
  • HTML extraction: retrieve markup or content from the DOM; a screenshot does not provide HTML.

The examples below use Playwright’s JavaScript API. Check the API documentation for the version installed in your project: Playwright Page API.

Find the element with a locator

A locator describes which element you mean. Prefer a locator based on the page’s meaning or a deliberate testing contract over a long path through incidental markup. Playwright recommends user-facing locators such as role, label, text, and alt text, as well as test IDs where appropriate. See Playwright’s locator guidance.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

Use role and accessible name for controls

For an accessible button named “Save,” target its role and name:

const saveButton = page.getByRole('button', { name: 'Save' });

This expresses the intended control more clearly than a selector tied to its current nesting or styling classes.

Use a label, text, alt text, or test ID when it fits

  • For a form control, use its associated label when available.
  • For meaningful visible content, use text.
  • For an image, use its descriptive alt text when available.
  • Use a test ID when the site provides one as an explicit testing contract.

Choose the locator that best describes the element’s intended identity. Text can change with copy edits or localization, and test IDs only help when the page deliberately exposes and maintains them.

Use CSS or XPath deliberately

In Selenium, prefer an ID when it is unique and predictable; otherwise, use a well-written CSS selector. Selenium supports XPath too, but its syntax can be harder to debug. Keep either selector focused on meaningful attributes where possible, rather than building a brittle chain of ancestors and child positions. See Selenium’s locator tips.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

Check that the locator identifies the intended element

A single-element action should not silently target an arbitrary match. Playwright’s single-element operations enforce strictness and throw if a locator matches multiple elements. If that happens, narrow the locator by using a meaningful attribute or starting within the relevant parent region.

Playwright also provides positional methods such as first(), last(), and nth(). Use them only when position is genuinely part of the rule—for example, when the requirement explicitly concerns the third item in a defined list. Otherwise, a page change can make the same position refer to a different element.

Locators are resolved against the current page when used, which helps when a page re-renders. That does not make a locator based on unstable markup or ambiguous text reliable; choose and validate the locator itself.

Capture the element or the whole page with Playwright

Capture one element

After locating the element, call the locator’s screenshot method:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
const saveButton = page.getByRole('button', { name: 'Save' });
await saveButton.screenshot({ path: 'save-button.png' });

The resulting image is the rendered element, not an export of its HTML.

Capture the scrollable page

Use the Page screenshot API when you want the page rather than the selected element:

await page.screenshot({ path: 'full-page.png', fullPage: true });

See the Page API documentation for screenshot behavior and options.

Extract HTML instead of taking a screenshot

If your goal is markup or text rather than pixels, use a DOM-reading operation for the located element. For example, Playwright’s locator API supports evaluating an expression against the matched element:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
const saveButton = page.getByRole('button', { name: 'Save' });
const html = await saveButton.evaluate(element => element.outerHTML);
const text = await saveButton.innerText();

outerHTML includes the element’s markup, while innerText() reads its rendered text. Neither is a screenshot; use the capture method that matches the output you need.

Or skip the browser setup

ScreenshotNeo takes a screenshot with one GET request. Its API documentation describes the available parameters; the example below captures the page at the URL as a WebP image:

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

ScreenshotNeo accepts cookie or consent banners before capture and removes more than 60 known consent platforms, newsletter popups, and chat widgets; those steps can be turned off. Bot checks and CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed. It also provides an MCP server with screenshot, page-information, and PDF-capture tools for AI agents. The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000. See ScreenshotNeo for product details, or sign up free.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common targeting problems

The locator matches more than one element

Use a more specific accessible name, label, or meaningful attribute, or scope the locator to the correct parent region. Avoid choosing a match by position unless position is part of the intended rule.

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

The locator matches nothing

Check that the page has loaded the content you expect and that the locator reflects the element’s current accessible name, text, label, or attributes. If the page has changed, inspect its current semantics and identifiers instead of extending a selector through unrelated DOM levels.

Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

The screenshot captures the wrong scope

Use a locator screenshot for one element. Use the Page screenshot API with fullPage: true for the full scrollable page. If you need markup, read it from the DOM instead.

A positional selector breaks after a page update

Replace the position with a locator based on the element’s meaning or a stable, explicit contract. Keep positional selection only when the page’s ordering is itself the requirement.

Do not confuse locators with CSS :target

The CSS :target pseudo-class is unrelated to browser-automation locators. It matches the document element identified by the URL fragment—for example, the element identified by #setup when the URL ends in #setup. It is for styling or selecting the fragment target in CSS, not a general way to choose an element for capture. See MDN’s :target reference.

Free tools Windows power users keep installed

One-click scans. No signup 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.