Skip to main content
🚀 See how 20+ AI Agents can make your QA faster and smarter. Join 10k+ QAs at global leadership summit! Register Now
No Result Found
Connect & Get help from fellow developers on our Discord community. Ask the CommunityAsk the Community

Screenshot a single element

Capture a percy snapshot of a specific element using the percySnapshot method.

In some cases, capturing the entire page isn’t necessary especially when you are only interested in a specific region or want to avoid dynamic sections of the page. For these situations, you can use the scope snapshot option to capture a single element instead of the full page.

Pass the scope option as a per-snapshot configuration, and Percy will capture only the scoped element at the given widths. The scope selector accepts any valid selector you can pass to document querySelector.

If there are multiple matching selectors on the page, Percy will select the first matching element.

Screenshot a single element using selector

Copy icon Copy
Copy icon Copy
Copy icon Copy
Copy icon Copy
Copy icon Copy

Screenshot a single element using xpath

Copy icon Copy
Copy icon Copy
Copy icon Copy
Copy icon Copy
Copy icon Copy

Screenshot a scrollable single element

Copy icon Copy
Copy icon Copy
Copy icon Copy
Copy icon Copy
Copy icon Copy

Use cases

Multiple elements with the same selector

If you would like to scope a screenshot to a specific element that has the same matching selector as other elements on the page you’ll have to get more specific with your selector. This can be done by either adding another unique selector to that element or by using standard CSS selectors to get more specific. This is the same way you would write CSS – Percy doesn’t add anything to this process.

For example, given the below DOM:

Copy icon Copy

Instead of using just .underline to select the element, you would want to either specify the element type (h1 / p) or by using CSS tree-structural pseudo-classes like :last-of-type or :nth-child.

Copy icon Copy

Likewise,

  • p.underline scope selects the ‘first’ paragraph
  • p.underline:last-of-type scope selects last paragraph

Selector for elements serialized by Percy

It may happen that your single-element screenshots are not working correctly with canvas/video elements.

During DOM serialization <canvas>, <video> elements are converted to <img>. Check canvas elements in Percy’s SDK Workflow. This can cause the selector to not match, and an incorrect screenshot.

To work around this, we can use CSS tree structural pseudo-classes as suggested in the previous section.

Copy icon Copy
  • Instead of canvas or video you’ll need to change it to img
  • If there is only a single canvas/video tag inside div we can directly use .selector as the scope.
Copy icon Copy

Likewise,

  • .selector img:nth-of-type(2) scope selects the ‘second’ canvas
  • .selector img:last-of-type scope selects the ‘last’ video

Usage when having Selenium's WebElement

When dealing with Web elements, you may use the below utility function that returns CSS selector i.e scope in our case, that could be easily passed to the percySnapshot function.

Make sure script eval is allowed in the test browser, Please check ref on how to verify.

Copy icon Copy
Copy icon Copy
Copy icon Copy

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback!

Talk to an Expert
Download Copy Check Circle