How to login to google account with playwright?

Viewed 4000

I have following source code and run it in headful mode. I can input email address. But, after that, there is message that saying "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.".

Do I need to set additional header or something else?

Here is my source code.

const playwright = require('playwright');
const cookiePath = '/home/ubuntu/.config/chromium/Default';
browser['chromium'] = await playwright['chromium'].launchPersistentContext(cookiePath,{
  headless: false,
  args: [
      `--disable-extensions-except=${pathToExtension}`,
      `--load-extension=${pathToExtension}`,
      ],
});
const page = await browser['chromium'].newPage();
const login_url = "https://accounts.google.com/signin/v2/identifier?hl=ja&flowName=GlifWebSignIn&flowEntry=ServiceLogin"; 
await page.goto(login_url);
await page.fill('#identifierId',userinfo['id']);
await page.click("#identifierNext");
await page.fill('[name=password]',userinfo['password']);
await page.click("#passwordNext");
2 Answers

My solution:

const { chromium } = require("playwright");

(async () => {
    const browser = await chromium.launch({
        headless: false,
        args: ["--disable-dev-shm-usage"],
    });
    const context = await browser.newContext({});
    const page = await context.newPage();
    const navigationPromise = page.waitForNavigation({
        waitUntil: "domcontentloaded",
    });
    await page.setDefaultNavigationTimeout(0);
    await page.goto(
        "https://accounts.google.com/signin/v2/identifier?hl=en&flowName=GlifWebSignIn&flowEntry=ServiceLogin"
    );
    await navigationPromise;
    await page.waitForSelector('input[type="email"]');
    await page.type('input[type="email"]', "youremail");
    await page.click("#identifierNext");
    await page.waitForSelector('input[type="password"]', { visible: true });
    await page.type('input[type="password"]', "yourpassword");
    await page.waitForSelector("#passwordNext", { visible: true });
    await page.click("#passwordNext");
    await navigationPromise;
    //you are in

I think you can search for login to google with Puppeteer also.

This works for me:

const browser = await playwright.chromium.launch({
  ignoreDefaultArgs: ['--disable-component-extensions-with-background-pages']
})
Related