You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Selenium+JS自动化Gmail登录遇安全验证错误求助

解决Gmail登录自动化的"This browser or app may not be secure"错误

问题背景

使用JavaScript结合Selenium实现Gmail登录自动化时,收到错误提示:

This browser or app may not be secure. Learn more
Try using a different browser. If you’re already using a supported browser, you can try again to sign in.

已测试Chrome、Firefox、Edge等多款浏览器均出现该错误;禁用Chrome的chrome://flags中“Site isolation”选项无效;Chrome 15.x版本可正常运行,升级至117.0.5938.88后失效。

解决方案

1. 针对Chrome:通过CDP隐藏自动化特征

Google对Chrome的自动化检测严格,需通过Chrome DevTools协议(CDP)修改浏览器参数,模拟真实环境:

const { Builder, By, until } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');

async function loginGmail() {
  const chromeOptions = new chrome.Options();
  // 禁用自动化控制标识
  chromeOptions.addArguments('--disable-blink-features=AutomationControlled');
  // 设置真实用户代理
  chromeOptions.addArguments('--user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"');
  // 禁用扩展与沙箱(部分环境需开启)
  chromeOptions.addArguments('--disable-extensions');
  chromeOptions.addArguments('--no-sandbox');

  const driver = await new Builder()
    .forBrowser('chrome')
    .setChromeOptions(chromeOptions)
    .build();

  // 移除navigator.webdriver属性,避免被检测
  await driver.executeCdpCommand('Page.addScriptToEvaluateOnNewDocument', {
    source: `
      Object.defineProperty(navigator, 'webdriver', {
        get: () => undefined
      });
    `
  });

  await driver.get('https://www.gmail.com');

  // 显式等待邮箱输入框加载完成
  await driver.wait(until.elementLocated(By.id('identifierId')), 10000);
  const emailInput = await driver.findElement(By.id('identifierId'));
  await emailInput.sendKeys('prodkia2.aus');

  // 等待下一步按钮并点击
  const nextBtn = await driver.wait(until.elementLocated(By.xpath('//span[text()="Next"]/parent::button')), 10000);
  await nextBtn.click();

  // 后续密码输入逻辑同理,使用显式等待
}

loginGmail();

2. 针对Firefox:修改浏览器配置

Firefox需修改偏好设置隐藏自动化标识:

const { Builder, By, until } = require('selenium-webdriver');
const firefox = require('selenium-webdriver/firefox');

async function loginGmail() {
  const firefoxOptions = new firefox.Options();
  // 禁用webdriver标识
  firefoxOptions.setPreference('dom.webdriver.enabled', false);
  firefoxOptions.setPreference('useAutomationExtension', false);
  // 设置真实用户代理
  firefoxOptions.addArguments('--user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/117.0"');

  const driver = await new Builder()
    .forBrowser('firefox')
    .setFirefoxOptions(firefoxOptions)
    .build();

  await driver.get('https://www.gmail.com');

  // 显式等待元素加载
  await driver.wait(until.elementLocated(By.id('identifierId')), 10000);
  const emailInput = await driver.findElement(By.id('identifierId'));
  await emailInput.sendKeys('prodkia2.aus');

  const nextBtn = await driver.wait(until.elementLocated(By.xpath('//span[text()="Next"]/parent::button')), 10000);
  await nextBtn.click();
}

loginGmail();

3. 替换固定等待为显式等待

避免使用sleep()这类固定等待,改用Selenium的显式等待,既提升稳定性,也更贴近真实用户操作节奏,减少被检测的概率。

4. 两步验证账号需使用App Password

若你的Gmail账号开启了两步验证,直接使用原密码登录会失败,需前往Google账号设置生成App Password,用该密码替代原密码完成自动化登录。


原始代码

driver = new webdriver.Builder()
.forBrowser('firefox')
.build();

driver.get('https://www.gmail.com')
sleep(20000);

driver.findElement(By.xpath("//*[@id='identifierId']")).sendKeys("prodkia2.aus");
sleep(5000); // //*[@id="identifierId"]

driver.findElement(By.xpath('/html/body/div[1]/div[1]/div[2]/div/c-wiz/div/div[2]/div/div[2]/div/div[1]/div/div/button/span')).click();

内容的提问来源于stack exchange,提问作者user932061

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 06:22:46