如何使用playwright登录google帐户?

问题描述 投票:0回答:4

我有以下源代码并在 headful 模式下运行它。 我可以输入电子邮件地址。 但是,之后会出现消息“无法让您登录。为了保护您的利益,您无法从此设备登录。请稍后重试,或从其他设备登录。”。

我需要设置额外的标题还是其他东西吗?

这是我的源代码。

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");
node.js playwright
4个回答
3
投票

我的解决方案:

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

我想你也可以用 Puppeteer 搜索登录 google。


3
投票

这对我有用:

--disable-blink-features=AutomationControlled
添加到您的
args


0
投票

您还必须有一个最近的用户代理

所以,除了arg

--disable-blink-features=AutomationControlled
之外,你还可以添加这些

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

const browser = await chromium.launch({ 
    headless: false,
    args: [
      '--disable-blink-features=AutomationControlled',
      '--no-sandbox',         // May help in some environments
      '--disable-web-security', // Not recommended for production use
      '--disable-infobars',    // Prevent infobars
      '--disable-extensions',   // Disable extensions
      '--start-maximized',      // Start maximized
      '--window-size=1280,720'  // Set a specific window size
    ]
});

并确保在上下文中设置最新的用户代理之一,使用 npm 包“user-agents”生成一个

const ua = require('user-agents');

const context = await browser.newContext({
   userAgent: new ua().toString(),
   viewport: { width: 1280, height: 720 },
   deviceScaleFactor: 1,
});

const page = await context.newPage();

就我而言,将用户代理更新为较新的用户代理后,它终于起作用了,即使我的 ua 不是那么旧


-1
投票

这对我有用:

const browser = await playwright.chromium.launch({
  ignoreDefaultArgs: ['--disable-component-extensions-with-background-pages']
})
© www.soinside.com 2019 - 2024. All rights reserved.