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

使用Puppeteer登录页面时遇会话过期错误求助

Puppeteer登录时遭遇会话过期错误

问题详情

使用Puppeteer编写登录代码,在Chrome中运行时页面返回会话过期错误:

THE SESSION HAS EXPIRED
The page has been inactive for some time and so your session has been logged off for security reasons. Identify yourself to access your account again.

尝试清除浏览记录后问题仍未解决,相关代码如下:

async function login(page) {
  try {
    await page.goto(loginPageUrl);

    await page.waitForSelector(MAIL_SELECTOR)
    await page.click(MAIL_SELECTOR, { clickCount: 3 });
    await page.keyboard.type(mail);

    await page.waitForSelector(PASSWORD_SELECTOR, { visible: true });

    await page.click(PASSWORD_SELECTOR, { clickCount: 3 });
    await page.keyboard.type(password);

    await page.click(BUTTON_SELECTOR);

    await page.waitForNavigation()
    
  } catch (err) {
    console.log(err);
  }
}

// Puppeteer初始化代码
const browser = await puppeteer.launch({
  headless: false,
  defaultViewport: null,
});
const page = await browser.newPage();

await page.setExtraHTTPHeaders({
  'accept-language': 'en-US,en;q=0.9,hy;q=0.8'
});
login(page)

解决建议

  • 启用浏览器会话持久化:每次启动全新浏览器实例容易被网站判定为异常,指定userDataDir复用浏览器数据,维持会话连贯性:
const browser = await puppeteer.launch({
  headless: false,
  defaultViewport: null,
  userDataDir: './puppeteer_session' // 本地目录保存浏览器缓存和会话
});
  • 添加合理操作延迟:代码执行速度过快触发反爬机制,在输入、点击操作间添加短延迟模拟真实用户行为:
async function login(page) {
  try {
    await page.goto(loginPageUrl, { waitUntil: 'networkidle2' }); // 等待网络稳定

    await page.waitForSelector(MAIL_SELECTOR, { visible: true });
    await page.click(MAIL_SELECTOR, { clickCount: 3 });
    await page.waitForTimeout(500); // 输入前延迟
    await page.keyboard.type(mail);

    await page.waitForSelector(PASSWORD_SELECTOR, { visible: true });
    await page.click(PASSWORD_SELECTOR, { clickCount: 3 });
    await page.waitForTimeout(500);
    await page.keyboard.type(password);

    await page.waitForTimeout(300);
    await page.click(BUTTON_SELECTOR);

    await page.waitForNavigation({ waitUntil: 'networkidle2' });
  } catch (err) {
    console.log(err);
  }
}
  • 补充必要请求头:添加User-Agent模拟真实浏览器,避免被识别为爬虫:
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36');
  • 模拟用户交互细节:添加鼠标移动、滚动等操作,进一步降低被识别的概率:
// 点击邮箱输入框前,先移动鼠标到输入框位置
const mailBox = await page.$(MAIL_SELECTOR);
const boxRect = await mailBox.boundingBox();
await page.mouse.move(boxRect.x + boxRect.width/2, boxRect.y + boxRect.height/2);
await page.waitForTimeout(200);
await mailBox.click({ clickCount: 3 });

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 22:50:23