Prompt
How do I automate login and navigation in browser-based portals?
Latest observation
Nov 6, 2026
OpenAI APIWeb search: off
Automating login and navigation in browser-based portals is usually done with browser automation tools such as Playwright, Selenium, or Puppeteer. The best approach depends on whether you need simple form filling, complex multi-step navigation, or robust unattended runs.
1) Pick the right tool
- Playwright: modern, fast, reliable, great for dynamic sites and multi-browser support.
- Selenium: widely used, good ecosystem, works with many languages.
- Puppeteer: strong for Chrome/Edge automation, JavaScript-focused.
If you’re starting fresh, Playwright is often the easiest and most reliable.
2) Typical automation flow
- Open the portal URL.
- Wait for the login form to load.
- Fill username and password fields.
- Handle any extra steps:
- MFA/2FA
- CAPTCHA
- SSO redirect
- “Remember this device” prompts
- Click sign in.
- Wait for the post-login page.
- Navigate to the required section.
- Extract data or perform actions.
- Save session state if you want to avoid logging in every time.
3) Example with Playwright (JavaScript)
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch({ headless: false });
const page = await browser.newPage();
await page.goto('https://portal.example.com/login');
await page.fill('#username', process.env.PORTAL_USER);
await page.fill('#password', process.env.PORTAL_PASS);
await page.click('button[type="submit"]');
await page.waitForLoadState('networkidle');
await page.goto('https://portal.example.com/dashboard/reports');
// do more navigation or extraction here
await browser.close();
})();
4) Handling login challenges
Multi-factor authentication
If the portal uses MFA:
- Use official APIs or service accounts if available.
- If allowed, use a human-in-the-loop step to enter the code.
- For repeated automation, some portals support trusted devices or app passwords.
CAPTCHA
CAPTCHAs are intentionally designed to block automation. Avoid trying to bypass them. Instead:
- Use the provider’s API if available.
- Request an automation-friendly access method from the portal owner.
- Use manual intervention when needed.
SSO / federated login
For SSO systems like Okta, Azure AD, or Google Workspace:
- Automate the full redirect chain carefully.
- Sometimes storing authenticated session state works better than logging in each time.
- You may need to handle popup windows or same-site cookie restrictions.
5) Make it reliable
- Use stable selectors:
- Prefer
data-testid,aria-label, or unique IDs. - Avoid brittle XPath tied to layout.
- Prefer
- Add explicit waits:
- Wait for elements to appear before interacting.
- Wait for navigation after clicking login.
- Handle errors and retries:
- Network failures
- Session timeout
- Unexpected modals
- Run with environment variables or a secret manager for credentials.
- Log actions for debugging.
6) Save and reuse sessions
Many portals allow session reuse:
- In Playwright, save storage state after login.
- Reuse cookies/local storage on later runs.
Example:
// After login
await page.context().storageState({ path: 'state.json' });
Then later:
const context = await browser.newContext({ storageState: 'state.json' });
const page = await context.newPage();
7) Security and compliance
- Check the portal’s terms of service and your organization’s policies.
- Don’t store passwords in source code.
- Use secrets managers or encrypted environment variables.
- Limit automation privileges to what’s necessary.
8) When to use APIs instead
If the portal has an API, prefer it over browser automation. APIs are:
- Faster
- More reliable
- Easier to maintain
- Less likely to break when the UI changes
If you want, I can give you:
- a Playwright script template,
- a Selenium example, or
- help with a specific portal flow like SSO/MFA.