TechBuzzPro

Automate Screenshots of Indian School Websites with Playwright

· 4 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to use Playwright, a free browser automation tool, to take screenshots of Indian school websites automatically. You'll be able to capture full-page or viewport screenshots of multiple schools in one go, perfect for audits, archiving, or research. You'll need basic familiarity with the command line and JavaScript (or Python if you prefer).

What You'll Need

  • Node.js (version 16 or later) installed on your computer.
  • A code editor like VS Code (optional but helpful).
  • Basic knowledge of running commands in a terminal.
  • A list of school website URLs you want to capture.

Step-by-Step

  1. Set up a new project folder. Open your terminal and run mkdir school-screenshots then cd school-screenshots.
  2. Initialize npm. Run npm init -y to create a package.json file.
  3. Install Playwright. Run npm install playwright. This installs the Playwright library.
  4. Install the browser. Run npx playwright install chromium to download the Chromium browser that Playwright will control.
  5. Create a list of URLs. In your project folder, create a file named schools.txt and add one website URL per line, for example:
    https://www.example-school.ac.in
    https://www.another-school.edu.in
  6. Write the script. Create a file named screenshot.js and open it in your editor. Paste the following code:
const { chromium } = require('playwright');
const fs = require('fs');

(async () => {
  const urls = fs.readFileSync('schools.txt', 'utf8').split('\n').filter(u => u.trim());
  const browser = await chromium.launch();
  const context = await browser.newContext({ viewport: { width: 1280, height: 720 } });
  const page = await context.newPage();

  for (const url of urls) {
    try {
      await page.goto(url, { waitUntil: 'networkidle', timeout: 60000 });
      await page.waitForTimeout(2000); // let dynamic content load
      const filename = url.replace(/[^a-z0-9]/gi, '_') + '.png';
      await page.screenshot({ path: 'screenshots/' + filename, fullPage: true });
      console.log('Saved: ' + filename);
    } catch (err) {
      console.error('Failed for ' + url + ': ' + err.message);
    }
  }

  await browser.close();
})();
  1. Create the output folder. Run mkdir screenshots in the terminal.
  2. Run the script. Execute node screenshot.js. The script will open each URL, wait for the page to load, and save a full-page screenshot in the screenshots folder.
  3. Check the results. Open the screenshots folder to see the PNG files. Each screenshot is named after the website's domain (e.g., www_example-school_ac_in.png).

Common Mistakes

  • Not waiting for the page to load. Many school websites use slow servers or heavy images. If you don't wait, screenshots may be blank or incomplete. Use waitUntil: 'networkidle' and add an extra delay via waitForTimeout.
  • Using the wrong URL format. Ensure the URLs in schools.txt include https:// or http://. Without it, Playwright may fail.
  • Forgetting to install the browser. If you skip step 4, the script will error with "Executable doesn't exist".
  • Not creating the screenshots folder. The script won't create it automatically; you must run mkdir screenshots first.

Tips

  • Use a custom viewport. If you want mobile screenshots, change the viewport in the script to { width: 375, height: 667 }.
  • Capture only the visible area. To get just the top of the page, remove fullPage: true from the screenshot call.
  • Add a delay between screenshots. Some websites block rapid requests. Add await page.waitForTimeout(3000) after each screenshot to be polite.
  • Use a headless browser. The script already runs headless (no visible window), which is faster. If you want to watch it, add headless: false in launch().
  • Handle login walls. If a school site requires login, you'll need to add steps to fill a login form. That's more advanced, but Playwright can do it with page.fill() and page.click().

Troubleshooting

  • Script throws "Cannot find module 'playwright'" – Run npm install playwright again in the correct folder.
  • Browser doesn't launch – Run npx playwright install chromium to download the browser binary.
  • Screenshots are blank or white – The site might block headless browsers. Try launching with headless: false to see if it loads. Also increase the wait time.
  • Timeout errors – Some sites are very slow. Increase the timeout in goto() to 120000 (2 minutes).
  • URLs with spaces or special characters – The filename sanitizer in the script handles most cases, but if a URL contains characters like ?, they get replaced. That's fine.
  • Script stops after first error – The try/catch block prevents this. If you still see it, check that the error is not in the loop itself (e.g., a syntax error).

Frequently Asked Questions

Can I use Python instead of JavaScript?

Yes. Install Playwright for Python (pip install playwright) and write a similar script using sync_playwright.

How do I take screenshots of multiple pages on the same site?

Add more URLs to schools.txt, or modify the script to navigate to internal links using page.click().

Will this work for any school website?

Mostly, but some sites use anti-bot protections that may block automated browsers. You can try adding a user agent or using stealth plugins, but that's beyond this guide.

Can I schedule this to run daily?

Yes. Use a task scheduler like cron (Linux/Mac) or Task Scheduler (Windows) to run node screenshot.js at your desired time.

How do I save screenshots in a different format like JPEG?

Change the file extension and add type: 'jpeg' and optionally quality: 80 in the screenshot() options.

What if a website is down?

The script will log an error and continue to the next URL. You can also collect failed URLs in an array and print them at the end.