Capture a Hindi Government Webpage Screenshot as PNG in Python with Selenium
Use Selenium in Python to capture a Hindi government webpage as PNG. Set the browser viewport, save the file reliably, and handle full-page needs.
Use Selenium WebDriver to open the page’s Hindi-language URL and call driver.save_screenshot("screenshot.png"). This saves the current browser window as a PNG. Choose the Hindi page on the site itself or use its known Hindi route; government portals do not share one standard Hindi URL or loading behavior.
Capture the current browser window
Install Selenium, ensure a compatible Chrome browser and driver are available, and run this script. Replace the example URL with the target page’s actual Hindi URL.
from pathlib import Path
from selenium import webdriver
url = "https://example.gov.in/" # Replace with the actual Hindi page URL.
output = Path("screenshot.png").resolve()
driver = webdriver.Chrome()
try:
driver.set_window_size(1440, 1000)
driver.get(url)
saved = driver.save_screenshot(str(output))
if not saved or not output.is_file():
raise OSError(f"Screenshot was not saved: {output}")
print(f"Saved PNG to {output}")
finally:
driver.quit()
Selenium’s Python WebDriver API describes save_screenshot as saving the current window to a PNG file. It recommends a filename ending in .png; the method returns False if an I/O error occurs. The official WebDriver guide also demonstrates saving a screenshot and closing the driver with quit().
Choose the Hindi page and wait for its content
- Open the target government portal and select Hindi, or navigate to the known Hindi route.
- Use the final page URL in
url. A generic English homepage URL may redirect, remember a language preference, or show a language selector instead of the page you intend to capture. - Wait for page-specific content if the portal renders it after navigation. Selenium’s
driver.getwaits according to the browser’s page-load strategy, but it does not establish that every late-loading image, font, or JavaScript widget is ready. - Capture after the relevant content is visible. For a page with a known heading, use an explicit wait for that element instead of an arbitrary long sleep.
There is no universal selector or reliable delay for all government websites. Inspect the target page and choose a selector that identifies the content you need. For example:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# After driver.get(url), wait up to 20 seconds for a page-specific heading.
WebDriverWait(driver, 20).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "main h1"))
)
saved = driver.save_screenshot(str(output))
Use a selector that exists on the selected page; main h1 is only an example. If the page has no such heading, choose another stable element or rely on a page-specific readiness condition.
Viewport size, PNG output, and full-page capture
save_screenshot captures the current browser window. Setting the window size before navigation makes the visible viewport more predictable, but the resulting image can still vary with browser version, operating system, fonts, responsive layout, and dynamic page content.
- Viewport screenshot: Use
driver.save_screenshot(path)for the visible browser window. - PNG file: Give the output path a
.pngextension and check both the Boolean return value and that the file exists. - Full document: A full-page screenshot is a separate capability. Selenium’s Firefox API documents
save_full_page_screenshot; confirm support for your browser, driver, and Selenium version before relying on it. The ordinary WebDriver screenshot method is for the current window.
For Firefox, the browser-specific API documents a full-document screenshot method in its Firefox WebDriver reference. Check the method and version requirements for your installed Selenium release before using it.
Check the result and keep the browser session clean
Use an absolute path when the script may run from different working directories. The example resolves the path before starting and verifies that the file exists. Put driver.quit() in a finally block so the browser session closes after either a successful capture or an exception.
If the screenshot is for review or archiving, open the PNG and confirm it shows the Hindi page, the expected language, and the intended viewport. Selenium can save the browser image; it cannot determine whether the chosen URL or page content is the correct one for your task.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| No PNG appears | The output directory does not exist, the process cannot write there, or the screenshot method returned False. |
Use a writable absolute path, create the parent directory if needed, check the Boolean return value, and verify output.is_file(). |
| The image shows English or a language selector | The URL is not the Hindi route, or the portal has not switched language. | Select Hindi on the site or use its known Hindi URL, then capture after the target page is displayed. |
| The screenshot is blank or missing page content | The page may still be loading, may render content asynchronously, or may have navigated to an error or interstitial page. | Inspect the browser page and URL. Wait for a stable, page-specific element before capturing and handle navigation failures explicitly. |
| Text or layout differs across machines | Viewport, browser, operating system, fonts, or responsive breakpoints differ. | Set a consistent window size and use a consistent browser environment. Verify that required fonts are available where the capture runs. |
| Only the visible portion is present | save_screenshot captures the current window rather than guaranteeing a full-document image. |
Use a browser-specific full-page method supported by your Selenium and browser versions, such as the documented Firefox API, or capture the page in sections. |
| Driver startup fails | The browser or driver is unavailable or incompatible with the environment. | Install a supported browser and driver setup for your environment and check the Selenium startup error for the missing or incompatible component. |
Performance, reliability, and cost
A single WebDriver capture requires starting or reusing a browser session, loading the page, and writing the PNG. Reuse a session when capturing multiple pages in one process, while still closing it with quit() when finished. Keep waits tied to the content required for the screenshot so slow third-party resources do not lead to arbitrary delays.
For repeatable results, keep the URL, viewport, browser environment, and readiness condition consistent. A successful file write confirms the screenshot was saved, not that the site served the expected language or that every dynamic element finished rendering. Selenium’s costs depend on the browser and infrastructure you run; the cited Selenium documentation does not establish a service price or capture benchmark.
Or skip the browser setup
ScreenshotNeo is a website screenshot API and MCP server. Make one request for a PNG; see the API documentation for options and response details.
import requests
r = requests.get(
"https://api.screenshotneo.com/v1/shot",
params={"access_key": "YOUR_API_KEY", "url": "https://example.gov.in/"},
timeout=90,
)
r.raise_for_status()
open("shot.png", "wb").write(r.content)
Replace the example URL with the target page’s Hindi URL. ScreenshotNeo accepts cookie and consent banners like a visitor and removes 60+ known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, and the response includes page-verdict and billing headers. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000 shots. All features are on every plan.
Sign up free for 1,000 screenshots a month, with no card required.
FAQ
Will Selenium translate an English government page into Hindi?
No. Selenium controls the browser and captures what the page displays. Open the portal’s Hindi version or select Hindi before saving the screenshot.
Does the PNG include the browser toolbar?
save_screenshot captures the browser’s current window. For a page screenshot, use a browser window without unrelated application chrome in the capture area and check the saved image.
Can I save a full-page PNG with the same method?
The standard method documents the current window. Full-document capture depends on browser-specific support; Selenium documents a Firefox-specific method, so confirm compatibility for your setup.


