How to Screenshot a Logged-In Page on an Indian Government Website with Selenium
Use Selenium to log in through a permitted flow, wait for the page’s content, and save a PNG from the same browser session.
To screenshot a logged-in page with Selenium, authenticate through the website’s permitted login flow, keep using that same WebDriver browser session, open the page you are allowed to view, wait for a page-specific signal that its content is ready, and save a PNG with driver.save_screenshot(...). Selenium automates a browser and provides screenshot methods; it does not grant permission to access a portal or bypass its login controls.
Government websites have different login steps and rules. Check the specific portal’s terms and instructions before automating access or storing a screenshot. If an approved flow requires a person to complete a CAPTCHA or multi-factor step, complete it normally in the same browser session.
1. Install Selenium and prepare a browser
This example uses Python and Chrome. Install Selenium in your project’s environment:
python -m pip install selenium
Selenium 4 can manage supported browser drivers through Selenium Manager when a compatible browser is installed. If your environment manages browser drivers separately, configure the driver according to that environment. Use a current browser and Selenium version compatible with your operating system.
2. Log in through the permitted flow and capture the page
The script below assumes you have authenticated in the browser session before opening the target page. Replace the example URL and CSS selector with the permitted page and a stable marker that appears when its relevant content is ready.
from pathlib import Path
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
TARGET_URL = "https://example.gov.in/account/page"
READY_SELECTOR = "main h1" # Replace with a stable marker on the actual page.
OUTPUT_PATH = Path("logged_in_page.png")
options = webdriver.ChromeOptions()
# Add only settings permitted by the portal and your organization.
driver = webdriver.Chrome(options=options)
try:
# Use the site's normal login process in this browser session.
# If a person must complete an approved login step, do so in this session.
driver.get("https://example.gov.in/login")
input("Complete the permitted login in the browser, then press Enter here: ")
# Go to a page you are authorized to view, using the same session.
driver.get(TARGET_URL)
# Wait for content that indicates this specific page is ready.
WebDriverWait(driver, 30).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, READY_SELECTOR))
)
OUTPUT_PATH.parent.mkdir(parents=True, exist_ok=True)
saved = driver.save_screenshot(str(OUTPUT_PATH))
if not saved:
raise RuntimeError(f"Screenshot could not be saved to {OUTPUT_PATH}")
print(f"Saved screenshot: {OUTPUT_PATH.resolve()}")
finally:
driver.quit()
This is a template, not a script verified against any particular government portal. Login fields, redirects, session duration, multi-factor steps, and page markers vary. The interactive pause lets a person complete a permitted step in the same browser session; it does not automate or circumvent that step. If your approved workflow already handles authentication, replace the pause with that workflow while keeping navigation and capture in the authenticated session.
3. Choose a readiness condition that matches the page
A successful navigation does not prove that JavaScript-rendered content is ready. Selenium’s page-load strategy waits for a document readiness state, but the page can continue changing afterward. Use an explicit wait for a condition tied to the content you intend to capture, such as:
- An authenticated-only heading or account marker is visible.
- A known record identifier or page title appears.
- A loading indicator disappears and the result area becomes visible.
- A specific table row or other target content is present.
Choose a selector that is stable on the portal and specific enough to distinguish the populated page from an empty shell. A generic main element may become visible before its contents finish loading. Selenium cautions that mixing implicit and explicit waits can produce unpredictable timing; this example uses an explicit wait only. Avoid fixed sleeps when a meaningful condition can be checked.
4. Capture the right scope
driver.save_screenshot("logged_in_page.png") saves a PNG screenshot of the current browser window and returns a boolean indicating whether saving succeeded. Use a writable destination path, and check that return value.
If you need one component rather than the browser window, Selenium also supports element screenshots in its language bindings. In Python, after locating the desired element:
element = driver.find_element(By.CSS_SELECTOR, "section.account-summary")
element.screenshot("account-summary.png")
Use a window screenshot when the surrounding page matters; use an element screenshot when only a specific component is needed. Confirm that the element is visible and fully populated before capturing it. The exact bounds and rendering depend on the browser and page layout.
5. Handle sessions and sensitive data carefully
- Keep authentication and navigation in the same WebDriver session. Starting a second browser session will not automatically carry over the first session’s authenticated state.
- Do not put passwords, session cookies, authorization values, or sensitive screenshots in source control, shared artifacts, or public logs.
- Use a destination directory with access controls appropriate for the screenshot’s contents, and follow the portal’s instructions and your organization’s data-handling requirements.
- Do not bypass CAPTCHA, multi-factor authentication, rate limits, or access restrictions. GIGW security guidance discusses login CAPTCHA and secure, HTTP-only cookies as security measures; it does not grant blanket permission for automated access.
The National Portal of India describes its own session cookie as managing that portal’s session and being deleted on logout or when leaving the site. That description applies to that portal and should not be assumed for other government websites.
6. Troubleshooting
| Symptom | Likely cause | What to do |
|---|---|---|
| The page redirects to login | The login did not complete, the session expired, or navigation happened in another browser session. | Complete the permitted login in the same WebDriver session, then navigate again. Check the portal’s normal session rules. |
| The wait times out | The selector is wrong, the page is still loading, the content is absent, or the session lacks access. | Inspect the page state in the browser, choose a marker that appears on the intended page, and allow an appropriate timeout. Do not treat a timeout as a reason to bypass access controls. |
| The screenshot is blank or incomplete | The capture ran before asynchronous content rendered, or the selected marker appeared before the target content. | Wait for a more specific marker, such as the actual record or completed result, then capture. Avoid relying only on navigation completion. |
save_screenshot returns false or raises a file error |
The path is not writable, its parent directory does not exist, or the process lacks permission. | Create the parent directory, choose a writable path, and check the returned boolean. |
| Browser or driver fails to start | The browser is missing, incompatible, or unavailable to the runtime. | Install a supported browser, check compatibility, and configure the driver using your environment’s approved setup. |
| The browser closes before manual login is complete | The script reached cleanup or was interrupted before the person completed login. | Keep the process running while completing the permitted step, then continue in the same browser session. |
| An element screenshot fails | The element could not be located, is not visible, or has not finished rendering. | Wait for that element to be visible and verify the selector before calling its screenshot method. |
7. Performance, reliability, and cost
Browser startup and page rendering usually dominate the work in a one-page capture. Reuse the authenticated session for navigation and capture rather than starting another browser between those steps. Wait for the specific content you need: a condition-based wait avoids both capturing too early and spending time in an unnecessarily long fixed delay.
For repeatable runs, make the readiness condition explicit, use a sensible timeout, save to a known writable path, and treat a failed save or missing page marker as an error. A screenshot records the browser’s rendered state at capture time; it does not prove that the page’s data is current or complete. Selenium itself is browser automation software; any browser, hosting, or operational costs depend on the environment where it runs.
Or skip the browser setup
ScreenshotNeo is a website screenshot API and MCP server. For a page that is publicly accessible to the API, one GET request returns a screenshot; an API call does not inherit your Selenium login session, so do not use it for a private logged-in page unless you have an authorized way to provide access and the portal permits it. See the ScreenshotNeo API documentation.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.gov.in -o shot.webp
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()
with open("shot.webp", "wb") as f:
f.write(r.content)
const q = new URLSearchParams({
access_key: 'YOUR_API_KEY',
url: 'https://example.gov.in'
});
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`Screenshot request failed: ${res.status}`);
const fs = await import('node:fs/promises');
await fs.writeFile('shot.webp', Buffer.from(await res.arrayBuffer()));
ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets before capture. Bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents use screenshot tools. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000 screenshots. Sign up for 1,000 free screenshots a month, with no card required.
FAQ
Does Selenium log in to every government portal the same way?
No. Login steps and automation rules vary by site. Follow the specific portal’s permitted process and keep any required human verification in the same browser session.
Can I capture a private page through ScreenshotNeo’s URL-only call?
The example call does not carry over Selenium’s authenticated browser session. Use it only where the API can access the page through an authorized method that the portal permits.
Can I save the screenshot as JPEG instead of PNG?
The Python WebDriver method shown here saves a PNG. If another format is required, use an image conversion step after capture and follow the handling rules for the resulting file.


