Return to Blog List
Handling Dynamic Content in Screenshots
JavaScript

Handling Dynamic Content in Screenshots

April 24, 2025
6 min read

Modern websites are increasingly dynamic, with content that loads asynchronously, relies on JavaScript, or appears based on user interactions. Capturing accurate screenshots of such content presents unique challenges that our Screenshot API is designed to solve.

The Challenge of Dynamic Content

When capturing screenshots of dynamic websites, several issues can arise:

  • Content may load after the initial page render
  • Elements might appear only after JavaScript execution
  • Animations might be in progress during capture
  • User interactions may be required to reveal content

Waiting for Content to Load

Our API provides several mechanisms to ensure content is fully loaded before capturing:

javascript
01// Wait for network activity to complete
02const response = await fetch('https://api.screenshotapi.com/capture', {
03 method: 'POST',
04 headers: {
05 'Content-Type': 'application/json',
06 'Authorization': 'Bearer YOUR_API_KEY'
07 },
08 body: JSON.stringify({
09 url: 'https://dynamic-site.com',
10 waitForEvent: 'networkidle0',
11 timeout: 30000 // 30 seconds
12 })
13});

Waiting for Specific Elements

For content that appears after certain conditions are met:

javascript
01// Wait for a specific element to appear in the DOM
02const response = await fetch('https://api.screenshotapi.com/capture', {
03 method: 'POST',
04 headers: {
05 'Content-Type': 'application/json',
06 'Authorization': 'Bearer YOUR_API_KEY'
07 },
08 body: JSON.stringify({
09 url: 'https://dynamic-site.com',
10 waitForSelector: '#content-loaded',
11 selectorTimeout: 10000 // 10 seconds
12 })
13});

Executing Custom JavaScript

For the most complex scenarios, you can execute custom JavaScript before capturing:

javascript
01const response = await fetch('https://api.screenshotapi.com/capture', {
02 method: 'POST',
03 headers: {
04 'Content-Type': 'application/json',
05 'Authorization': 'Bearer YOUR_API_KEY'
06 },
07 body: JSON.stringify({
08 url: 'https://dynamic-site.com',
09 executeBeforeScreenshot: `
10 // Click a button to reveal content
11 document.querySelector('#show-more-button').click();
12
13 // Wait for animation to complete
14 await new Promise(resolve => setTimeout(resolve, 1000));
15
16 // Scroll to ensure all content is in view
17 window.scrollTo(0, 500);
18 `
19 })
20});

By leveraging these techniques, you can ensure your screenshots accurately represent even the most dynamic web content, providing your users with the most valuable and accurate visual information.

Dynamic ContentAdvanced

Ready to Get Started?

Get your API key now and start capturing screenshots in minutes.