TechBuzzPro

Capture Hindi Web Pages with Playwright Screenshots

· 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

Taking screenshots of Hindi web pages with Playwright is straightforward once you handle two things: the correct font rendering for Devanagari script and the page's full height. This guide shows you how to set up a Playwright script that captures full-page screenshots of any Hindi site, with proper text rendering and file naming.

What You'll Need

  • Node.js (version 16 or later) installed on your machine
  • A code editor (VS Code, Sublime, etc.)
  • Basic familiarity with the command line
  • An internet connection to install packages and load the web page

Step-by-Step

  1. Create a new project folder – Open your terminal and run mkdir hindi-screenshots then cd hindi-screenshots.
  2. Initialize the project – Run npm init -y to create a package.json file.
  3. Install Playwright – Run npm install playwright. This installs the library but not the browsers yet.
  4. Install the Chromium browser – Run npx playwright install chromium. You can also install Firefox or WebKit, but Chromium is recommended for best Devanagari rendering.
  5. Create the script file – Create a file named capture.js in the project folder.
  6. Write the basic script – Open capture.js and add the following code:
    const { chromium } = require('playwright');
    
    (async () => {
      const browser = await chromium.launch();
      const page = await browser.newPage();
      await page.goto('https://example.com/hindi-page', { waitUntil: 'networkidle' });
      await page.screenshot({ path: 'screenshot.png', fullPage: true });
      await browser.close();
    })();
        
  7. Replace the URL – Change 'https://example.com/hindi-page' to the actual Hindi page you want to capture.
  8. Run the script – In the terminal, run node capture.js. This will take a full-page screenshot and save it as screenshot.png in the same folder.
  9. Check the output – Open the PNG file. If Hindi text appears as boxes or missing glyphs, proceed to the next steps to fix font rendering.

Common Mistakes

  • Not waiting for fonts to load – Hindi web fonts (like Noto Sans Devanagari) load asynchronously. Using waitUntil: 'networkidle' helps, but sometimes you need an explicit wait. Add await page.waitForTimeout(2000); before the screenshot.
  • Using the default viewport – The default viewport is 1280x720, which may cut off content. Set a larger viewport or use fullPage: true to capture the entire page.
  • Ignoring system fonts – If your OS lacks Devanagari fonts, the browser may fall back to a font that doesn't render properly. Install a font like Noto Sans Devanagari on your system, or use Playwright's launch with args: ['--font-render-hinting=none'].
  • Not handling dynamic content – Some Hindi pages load content via JavaScript after the initial load. Use waitForSelector to wait for a specific element that indicates content is ready.

Tips

  • Use a custom viewport – Set viewport: { width: 1920, height: 1080 } in newPage() to capture a desktop-sized screenshot.
  • Name files with the page title – Extract the page title using await page.title() and include it in the filename for easier identification.
  • Capture specific elements – If you only need a section, use page.locator('selector').screenshot() instead of the whole page.
  • Use device scale factor – For high-DPI screenshots, set deviceScaleFactor: 2 in the viewport options to get a sharper image.
  • Add a delay for animations – If the page has carousels or animations, wait a few seconds before capturing to get a stable state.

Troubleshooting

  • Hindi text appears as boxes (tofu) – This means the font is missing. Install a Devanagari font on your system, or embed a web font in the page if you control it. Alternatively, use page.emulateMedia({ fonts: { families: ['Noto Sans Devanagari'] } }) to force a font.
  • Page loads but screenshot is blank – The page might be using a canvas or WebGL. Wait for a specific element or use page.waitForLoadState('networkidle') after navigation.
  • Script times out – Some slow pages need a longer timeout. Add timeout: 60000 to page.goto().
  • Full-page screenshot is cut off – Some pages have sticky headers that overlap content. Try scrolling to the bottom first: await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight)); then take the screenshot.
  • Playwright not found – Ensure you installed it in the same directory as your script. Run npm ls playwright to verify.

Frequently Asked Questions

  1. Can I use Playwright with Python instead of Node.js? – Yes, Playwright supports Python. The steps are similar: install playwright via pip, then use sync_playwright() to write the script.
  2. Does Playwright support all Hindi websites? – It supports any website that runs in Chromium, Firefox, or WebKit. Some sites may block automated access; use a user-agent or headful mode to avoid detection.
  3. How do I save screenshots in JPEG format? – Change the path extension to .jpg and add type: 'jpeg' in the screenshot options, e.g., { path: 'shot.jpg', type: 'jpeg', quality: 80 }.
  4. Can I capture a specific element like a table? – Yes, use page.locator('table').screenshot({ path: 'table.png' }) to capture only that element.
  5. Why does my screenshot have a white background instead of the page's background? – This happens if the page uses a dark mode or has a transparent background. Set page.emulateMedia({ colorScheme: 'light' }) to force light mode, or set the background color in the screenshot options.
  6. How do I take a screenshot of a page that requires login? – Use Playwright to fill in the login form first, then navigate to the desired page and screenshot. Store credentials in environment variables for security.