Web Automation with Playwright: Combining Manual Login and a Modular Loop
How to organize a modular Playwright script in Node.js using visible-browser manual login, Set deduplication, and resilient UI recovery.
Beyond E2E test suites, Playwright is great for exploring how browser scripts handle dynamic lists, lazy scroll loading, and modal state. In playwright-linkedin (https://github.com/rniguel/playwright-linkedin), I structured that workflow into focused Node.js modules.
Manual Login Instead of Hardcoded Credentials
Putting passwords inside .env often breaks on 2FA prompts or extra security checks. Opening Chromium in visible mode (headless: false) and letting the user sign in manually keeps credentials out of code while handing over an authenticated session to Playwright:
async function login(page) {
console.log("[auth] 👉 Faça login manualmente");
await page.goto("https://www.linkedin.com/login");
await page.waitForTimeout(30000);
console.log("[auth] ✅ Login concluído");
}Modular Loop & UI Error Recovery
Splitting logic across src/auth.js, src/search.js, src/connection.js, and src/pagination.js keeps linkedin.js clean. Three patterns stand out for any DOM automation:
1. Keep a Set (sentSet) so updated DOM lists do not trigger duplicate clicks.
2. Inspect aria-label attributes instead of brittle generated CSS class names.
3. Wrap per-item actions in try/catch and press Escape to dismiss stuck dialogs before continuing.
while (true) {
const buttons = await getConnectButtons(page);
const count = await buttons.count();
let foundNew = false;
for (let i = 0; i < count; i++) {
try {
const btn = buttons.nth(i);
const label = ((await btn.getAttribute("aria-label")) || "").trim();
if (!label.toLowerCase().includes("conectar")) continue;
const processed = await processConnection(
page, btn, label, sentSet, config.targetCompany
);
if (processed) foundNew = true;
} catch (err) {
console.log("[app] ❌ Erro nesse perfil");
try {
await page.keyboard.press("Escape");
} catch {}
await delay(2000);
}
}
if (!foundNew) break;
}Final Thoughts
Working with accessible attributes like aria-label and defensive error recovery carries straight over into building stronger Playwright E2E tests as well. When automating against third-party sites, always respect platform rate limits and terms of service.
Miguel Riquelme
Front-end Engineer. Software Engineering MBA student at USP/Esalq.