Capture Hindi Web Pages with Playwright Screenshots
In this article
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
- Create a new project folder – Open your terminal and run
mkdir hindi-screenshotsthencd hindi-screenshots. - Initialize the project – Run
npm init -yto create apackage.jsonfile. - Install Playwright – Run
npm install playwright. This installs the library but not the browsers yet. - Install the Chromium browser – Run
npx playwright install chromium. You can also install Firefox or WebKit, but Chromium is recommended for best Devanagari rendering. - Create the script file – Create a file named
capture.jsin the project folder. - Write the basic script – Open
capture.jsand 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(); })(); - Replace the URL – Change
'https://example.com/hindi-page'to the actual Hindi page you want to capture. - Run the script – In the terminal, run
node capture.js. This will take a full-page screenshot and save it asscreenshot.pngin the same folder. - 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. Addawait 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: trueto 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
launchwithargs: ['--font-render-hinting=none']. - Not handling dynamic content – Some Hindi pages load content via JavaScript after the initial load. Use
waitForSelectorto wait for a specific element that indicates content is ready.
Tips
- Use a custom viewport – Set
viewport: { width: 1920, height: 1080 }innewPage()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: 2in 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: 60000topage.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 playwrightto verify.
Frequently Asked Questions
- Can I use Playwright with Python instead of Node.js? – Yes, Playwright supports Python. The steps are similar: install
playwrightvia pip, then usesync_playwright()to write the script. - 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.
- How do I save screenshots in JPEG format? – Change the
pathextension to.jpgand addtype: 'jpeg'in the screenshot options, e.g.,{ path: 'shot.jpg', type: 'jpeg', quality: 80 }. - Can I capture a specific element like a table? – Yes, use
page.locator('table').screenshot({ path: 'table.png' })to capture only that element. - 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. - 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.