ScreenshotNeo

BlogHow-to

Capture Indian Train Booking Webpage Screenshots with Python Playwright After Login

Learn how to capture screenshots after login with Playwright Python in an authorized test environment, and why IRCTC’s terms rule out automating its live site.

By the ScreenshotNeo team4 October 20268 min read

Direct answer: Playwright for Python can reuse an authenticated browser context and save a regular, full-page, or element screenshot. But IRCTC’s terms state that “Use of automation software and/or Scripting Software is strictly prohibited.” Do not use Playwright to log in to or capture the live IRCTC booking site. Use the manual capture steps below for the live site, or run the Playwright example only against a local demo or another site whose owner has explicitly permitted automation. IRCTC terms · Playwright authentication documentation · Playwright screenshot documentation

What is permitted

IRCTC’s published terms prohibit automation and scripting on its website and mobile applications. The terms also describe scripting tools as suspicious booking activity that may lead to account deactivation and ticket release. The official guide shows a user selecting LOGIN and entering a registered user ID and password; its illustrated login panel includes CAPTCHA. That guide describes a user-facing login, not permission to automate it. Its interface images may be dated.

  • For the live IRCTC site: sign in and take the screenshot manually using your browser or operating system’s capture feature. Do not script login, CAPTCHA handling, page navigation, or screenshot capture.
  • For an authorized test or demo site: you may use Playwright’s general authentication and screenshot APIs if the site owner permits automation.

The example below demonstrates the generic Playwright workflow against a local test page. It does not connect to IRCTC. Replace the local page and login selectors only with those belonging to an environment where you are authorized to automate.

Capture a screenshot after login in an authorized test environment

1. Install Playwright

python -m venv .venv
# macOS or Linux:
source .venv/bin/activate
# Windows PowerShell:
# .venv\Scripts\Activate.ps1
python -m pip install playwright
python -m playwright install chromium

Start a local test application that you control, with a login page at http://127.0.0.1:8000/login and an authenticated page at http://127.0.0.1:8000/account. The selectors and field names below are examples for that fictional local app. Adjust them to match your authorized test application.

2. Sign in once and save browser state

# save_auth.py
import os
from pathlib import Path
from playwright.sync_api import sync_playwright

LOGIN_URL = "http://127.0.0.1:8000/login"
STATE_FILE = Path("playwright/.auth/state.json")

username = os.environ["TEST_USERNAME"]
password = os.environ["TEST_PASSWORD"]

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    context = browser.new_context()
    page = context.new_page()
    page.goto(LOGIN_URL, wait_until="domcontentloaded")
    page.get_by_label("Email").fill(username)
    page.get_by_label("Password").fill(password)
    page.get_by_role("button", name="Sign in").click()
    page.wait_for_url("**/account")
    STATE_FILE.parent.mkdir(parents=True, exist_ok=True)
    context.storage_state(path=str(STATE_FILE))
    browser.close()

Set TEST_USERNAME and TEST_PASSWORD in your shell or secret manager before running the script. The labels, button name, and destination URL must match your test app. This example does not implement multi-factor authentication or CAPTCHA; use only a test flow designed and approved by the application owner.

3. Load that state and capture the page

# capture.py
from pathlib import Path
from playwright.sync_api import sync_playwright

STATE_FILE = Path("playwright/.auth/state.json")
TARGET_URL = "http://127.0.0.1:8000/account"

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    context = browser.new_context(storage_state=str(STATE_FILE))
    page = context.new_page()
    page.goto(TARGET_URL, wait_until="networkidle")
    page.screenshot(path="account.png", full_page=True)
    browser.close()

Run python save_auth.py and then python capture.py. The first script saves browser state after the test login; the second creates an isolated context using that state and writes a full-page PNG. For an ordinary viewport screenshot, remove full_page=True.

Choose the right capture scope

# Current viewport
page.screenshot(path="viewport.png")

# Entire scrollable page
page.screenshot(path="full-page.png", full_page=True)

# One element, selected using a locator
page.get_by_role("main").screenshot(path="main.png")

Element screenshots are useful for a receipt, result panel, or account summary in an authorized test page. Use a locator that uniquely identifies the intended element. Full-page captures may trigger lazy-loaded content as the page is laid out, but the page’s own behavior and network conditions affect what appears.

Authentication state and security

Playwright’s saved state can contain cookies and other credentials that allow access as the signed-in account. Treat the state file like a password:

  • Use a test account with only the access needed for the task.
  • Keep state files out of source control. Add the auth directory to .gitignore, for example: playwright/.auth/.
  • Restrict file permissions and avoid copying state into logs, tickets, artifacts, or shared folders.
  • Delete the file when it is no longer needed; expire or revoke the test session if appropriate.
  • Do not reuse a state file across people, environments, or production accounts.

Playwright documents reusing browser context state for later contexts. That capability does not grant permission to automate a site, and it does not guarantee a session will remain valid: applications can expire or revoke sessions.

Waits, page state, and screenshot options

Pick a wait condition that reflects the test app’s behavior. domcontentloaded waits for document parsing; load waits for the page load event; networkidle waits for network activity to become idle. A page that polls or keeps connections open may never become network-idle. For dynamic content, waiting for a meaningful element is often more dependable:

page.goto(TARGET_URL, wait_until="domcontentloaded")
page.get_by_role("heading", name="Account overview").wait_for(state="visible")
page.screenshot(path="account.png", full_page=True)

Playwright’s screenshot API supports viewport and full-page captures as well as screenshots of an element. The API accepts screenshot settings such as path and image type; consult its Python screenshot reference for the current options and behavior. Keep captures focused: a full-page image can be very tall, and content outside the viewport may be loaded or rendered differently by the page.

Manual capture on the live IRCTC site

  1. Open IRCTC in your normal browser and sign in yourself using its user-facing login flow.
  2. Navigate to the page you need and position it as required.
  3. Use your browser’s built-in screenshot or print feature, or your operating system’s screen-capture shortcut.
  4. Store the image securely. Booking pages can show personal or travel information; redact details before sharing.

IRCTC’s guide illustrates CAPTCHA in the login interface, but that is not an invitation to solve or bypass it with automation. The workflow here keeps interaction with the live service manual.

Troubleshooting

Symptom Likely cause Fix
Executable doesn't exist or browser launch fails The Playwright package is installed but its browser binary is missing. Run python -m playwright install chromium in the same environment as the script.
Login succeeds but the next script looks signed out The saved state was not loaded, was saved before login completed, or the app expired the session. Check the state file path, wait for a post-login URL or authenticated element before saving, then sign in again through the authorized test flow.
Locator times out The example selector does not match the test page, or the control is not yet available. Use the test app’s actual accessible label or role and wait for the control to appear. Do not adapt this into automated interaction with live IRCTC.
wait_until="networkidle" hangs The page continually makes network requests or maintains a connection. Navigate with domcontentloaded and wait for a specific visible element or a deliberate short delay appropriate to the test.
Screenshot is blank or missing expected content The page has not rendered the target content, the wrong route loaded, or the element is hidden. Assert the expected URL and wait for a visible landmark before capturing. Inspect the authorized test page in headed mode when diagnosing rendering issues.
Full-page screenshot omits lazy content The application loads content only after scrolling or user interaction. In an authorized test, scroll through the page or trigger the documented app behavior, then wait for the content and capture. Do not automate this on IRCTC.
Authentication state file is rejected The session expired, state belongs to another environment, or the application uses additional session checks. Generate fresh state using the approved test login and the same environment; verify the app’s supported test authentication flow.

Performance, reliability, and cost

Browser startup and page rendering usually take more time and resources than writing the image file. Reuse one browser process for several authorized test captures when appropriate, while creating a separate context per account or isolation boundary. Limit full-page captures to pages where they add value; tall pages produce larger images and may take longer to render. Set sensible timeouts in your test harness and make failures visible rather than silently saving partial output.

Reliability depends on the target app, session lifetime, network, fonts, image loading, and dynamic content. A screenshot is a point-in-time rendering, not proof that all page data loaded correctly. Wait for an app-specific ready condition and validate the output when screenshots feed a critical test or record.

Playwright is an open-source browser automation library; this workflow’s direct costs depend on the machine, browser runtime, and infrastructure you provide. IRCTC’s terms describe account and ticket consequences for suspicious activity, so the live-site automation approach is not an acceptable cost-saving shortcut.

Or skip the browser setup

For a page where you are authorized to request a screenshot, ScreenshotNeo provides a website screenshot API and MCP server. A single request returns an image or PDF. See the ScreenshotNeo API documentation.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
import requests

r = requests.get(
    "https://api.screenshotneo.com/v1/shot",
    params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
    timeout=90,
)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);

Use an API only for pages you are authorized to capture; this is not a way to automate IRCTC. ScreenshotNeo removes cookie banners, popups, and chat widgets before the shot. Bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000. Learn about ScreenshotNeo or sign up free for 1,000 screenshots a month, no card required.

FAQ

Can I use this code on IRCTC if I only take screenshots?

No. The cited IRCTC terms prohibit automation and scripting on the service. Capture pages on the live site manually; use Playwright only in an environment whose owner explicitly permits it.

Can Playwright save a screenshot of a page that requires authentication?

Yes, its general Python APIs can save and reload authenticated browser context state, then capture a page in that context. Session validity and site permission remain separate questions.

Does saving browser state include my password?

The saved state can include sensitive cookies and headers that may grant account access. Protect it as a secret even if it does not contain a plain-text password.

Can I automate an authorized staging copy of my own booking app?

Yes, if the owner or operator has explicitly authorized automation there. Keep test credentials and state isolated from production accounts.