Hindi Coaching Landing Page Previews with Node.js Playwright
In this article
This article explains how to generate preview images of Hindi coaching class landing pages using Node.js and Playwright, a browser automation library. You will learn the exact steps to capture full-page screenshots, handle dynamic content, and automate the process for multiple pages. The guide covers setup, code examples, common pitfalls, and practical tips for getting reliable previews.
Why Use Playwright for Landing Page Previews
Playwright is a Node.js library that controls Chromium, Firefox, or WebKit through a single API. Unlike simple HTTP requests that only fetch HTML, Playwright renders the full page in a real browser engine, executing JavaScript, loading fonts, and applying CSS. This makes it ideal for capturing accurate previews of modern landing pages that rely on client-side rendering, animations, or lazy-loaded images. For Hindi coaching sites, which often use custom fonts like Devanagari and embedded video, a headless browser is the only reliable way to get a pixel-perfect snapshot.
Compared to alternatives like Puppeteer or Selenium, Playwright offers better auto-waiting, built-in selectors, and a simpler API for screenshots. It also handles multiple browser contexts and mobile viewports out of the box, which is useful if you need previews for both desktop and mobile versions of a page.
Setting Up the Project
To start, you need Node.js (version 14 or later) installed on your machine. Create a new directory and initialize a project:
- Run
npm init -yto create a package.json file. - Install Playwright:
npm install playwright. - Install the browser binaries:
npx playwright install chromium(or all browsers if needed).
If you are behind a corporate proxy or have limited disk space, you can install only Chromium, which is sufficient for most previews. The installation downloads the browser to a local cache, so no system-wide changes are required.
Basic Screenshot Script
Here is a minimal script that opens a URL, waits for the page to load, and takes a full-page screenshot:
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage({ viewport: { width: 1280, height: 800 } });
await page.goto('https://example.com/hindi-coaching', { waitUntil: 'networkidle' });
await page.screenshot({ path: 'preview.png', fullPage: true });
await browser.close();
})();
The waitUntil: 'networkidle' option ensures that all network requests have finished, which is important for pages that load fonts or images asynchronously. However, if the page has continuous polling or live chat widgets, networkidle may never resolve; in that case, use 'load' plus a fixed delay.
Handling Dynamic Content and Delays
Hindi coaching landing pages often include video backgrounds, countdown timers, or carousels that change over time. To capture a stable preview, you may need to:
- Wait for specific elements to appear using
page.waitForSelector(). - Use
page.waitForTimeout()to let animations settle (e.g., 1000-2000 ms). - Disable animations via CSS injection if they interfere with the screenshot.
For example, to wait for the main heading to be visible:
await page.waitForSelector('h1', { state: 'visible' });
await page.waitForTimeout(500); // let fonts load
If the page uses a cookie consent banner that obscures content, you can either accept it via a click or hide it with a CSS rule. Playwright allows you to add style tags before capturing:
await page.addStyleTag({ content: '.cookie-banner { display: none !important; }' });
Automating Multiple Pages
For a coaching website with many courses or branches, you will likely want to generate previews for several URLs in one run. You can loop over an array of URLs and save each screenshot with a unique filename:
const urls = [
{ name: 'home', url: 'https://example.com' },
{ name: 'courses', url: 'https://example.com/courses' },
{ name: 'contact', url: 'https://example.com/contact' }
];
for (const item of urls) {
await page.goto(item.url, { waitUntil: 'networkidle' });
await page.screenshot({ path: `${item.name}.png`, fullPage: true });
}
To avoid hitting the server too hard, add a small delay between requests. You can also run multiple pages concurrently using Promise.all, but be mindful of memory usage.
Common Problems and Solutions
Several issues frequently arise when generating previews:
- Blank screenshots: This usually happens when the page uses client-side routing or requires a login. Ensure you are using the correct URL and wait for the content to render.
- Fonts not loading: Devanagari fonts can be large; use
waitUntil: 'networkidle'or explicitly wait fordocument.fonts.ready. - Inconsistent dimensions: Set a fixed viewport size and disable device scaling factor to get consistent output.
- Timeouts: Increase the default timeout in
page.goto()options if the page is slow.
If a page uses a service worker or a framework like Next.js, you may need to wait for hydration. A simple waitForTimeout(2000) often solves this.
Comparing with Other Tools
Playwright is not the only option. Puppeteer is similar but lacks some built-in conveniences like auto-waiting and multi-browser support. Selenium is more geared toward testing and requires a WebDriver. For pure screenshot generation, Playwright is often the easiest to set up because it bundles browser binaries and has a concise API. Tools like capture-website-cli or pageres are wrappers around Puppeteer and can be used if you prefer a command-line interface. However, if you need custom logic (e.g., logging in, filling a form, or taking multiple screenshots per page), writing a Playwright script gives you full control.
Cost and Performance Considerations
Running Playwright locally is free, but it consumes CPU and memory, especially when rendering multiple pages. For a few dozen previews, a typical laptop is sufficient. For bulk generation, consider using a cloud function or a CI pipeline. The main cost is time: each page may take 2-5 seconds to load and capture, depending on the page complexity and network speed. You can speed up the process by disabling images or using a faster connection, but that may degrade the preview quality.
Safety and Best Practices
When automating page visits, respect the website's terms of service and robots.txt. Do not send an excessive number of requests in a short time, as that could be considered abusive. Add a delay between requests and limit concurrency. Also, ensure that any screenshots you generate are for lawful purposes, such as internal review or personal use. If you plan to publish previews, check copyright and fair use policies.
Frequently Asked Questions
Can I generate previews without installing a browser?
No, Playwright requires a browser engine to render the page. You can use Playwright's browser binaries, which are downloaded during installation. There is no way to generate accurate screenshots without a browser.
How do I take a screenshot of a specific element, not the whole page?
Use page.locator('selector').screenshot({ path: 'element.png' }). This captures only that element, which is useful for highlighting a form or a course card.
Why does my screenshot show a cookie banner even though I waited?
Some banners appear after a delay or require interaction. You can either click the accept button using page.click() or hide the element with CSS as shown above.
Can I schedule previews to run automatically?
Yes, you can run the script with a cron job on Linux/macOS or Task Scheduler on Windows. Alternatively, use a CI service like GitHub Actions to run it on a schedule.
What if the page uses a login wall?
You can automate login by filling in credentials, but storing passwords in scripts is risky. Use environment variables and never commit them to version control. Alternatively, use a saved browser context or session cookie.
Does Playwright work with Hindi text rendering?
Yes, Playwright uses the system fonts, so if Devanagari fonts are installed, they will render correctly. On Linux servers, you may need to install font packages like fonts-noto to ensure proper display.