79504881

Date: 2025-03-12 21:30:04
Score: 0.5
Natty:
Report link

For future visitors who need to persist the session in Playwright after a manual Google login and avoid the block:

Couldn't sign you in. For your protection, you can't sign in from this device. Try again later, or sign in from another device.

I share this solution inspired by the answers from jwallet and Jose A, as their solutions did not work for me as of today separately.

This code allows you to manually log in to Google and save the session state for reuse in future executions. It uses Playwright Extra and a random User-Agent to minimize blocks.

import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const UserAgent = require('user-agents');
import { chromium } from 'playwright-extra';

const optionsBrowser = {
    headless: false,
    args: [
        '--disable-blink-features=AutomationControlled',
        '--no-sandbox',
        '--disable-web-security',
        '--disable-infobars',
        '--disable-extensions',
        '--start-maximized',
        '--window-size=1280,720',
    ],
};
const browser = await chromium.launch(optionsBrowser);

const optionsContext = {
    userAgent: new UserAgent([/Chrome/i, { deviceCategory: 'desktop' }]).userAgent,
    locale: 'en-US',
    viewport: { width: 1280, height: 720 },
    deviceScaleFactor: 1,
};

const context = await browser.newContext(optionsContext);
const page = await context.newPage();
await page.goto('https://accounts.google.com/');

// Give 90 seconds to complete the manual login.
const waitTime = 90_000;
console.log(`You have ${waitTime / 1000} seconds to complete the manual login...`);
await page.waitForTimeout(waitTime);

// Save the session
await page.context().storageState({ path: 'myGoogleAuth.json' });
console.log('Session saved in myGoogleAuth.json');

await browser.close();

Once the session is saved, you can reuse it by loading the myGoogleAuth.json file in future executions:

const context = await browser.newContext({ storageState: 'myGoogleAuth.json' });

This prevents having to log in every time you run the script.

If the session does not persist, check if Google has invalidated the cookies and try logging in again to generate a new storageState.

Reasons:
  • Blacklisted phrase (1): did not work
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Rodrigo de Miguel