Automate Screenshots of Indian School Websites with Playwright
In this article
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
- Set up a new project folder. Open your terminal and run
mkdir school-screenshotsthencd school-screenshots. - Initialize npm. Run
npm init -yto create a package.json file. - Install Playwright. Run
npm install playwright. This installs the Playwright library. - Install the browser. Run
npx playwright install chromiumto download the Chromium browser that Playwright will control. - Create a list of URLs. In your project folder, create a file named
schools.txtand add one website URL per line, for example:
https://www.example-school.ac.in
https://www.another-school.edu.in - Write the script. Create a file named
screenshot.jsand 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();
})();
- Create the output folder. Run
mkdir screenshotsin the terminal. - 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 thescreenshotsfolder. - Check the results. Open the
screenshotsfolder 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 viawaitForTimeout. - Using the wrong URL format. Ensure the URLs in
schools.txtincludehttps://orhttp://. 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 screenshotsfirst.
Tips
- Use a custom viewport. If you want mobile screenshots, change the
viewportin the script to{ width: 375, height: 667 }. - Capture only the visible area. To get just the top of the page, remove
fullPage: truefrom 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: falseinlaunch(). - 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()andpage.click().
Troubleshooting
- Script throws "Cannot find module 'playwright'" – Run
npm install playwrightagain in the correct folder. - Browser doesn't launch – Run
npx playwright install chromiumto download the browser binary. - Screenshots are blank or white – The site might block headless browsers. Try launching with
headless: falseto see if it loads. Also increase the wait time. - Timeout errors – Some sites are very slow. Increase the
timeoutingoto()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/catchblock 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.