Files
nanocode/playwright-hotspots/scripts/weibo_login_v2.js
T
Ubuntu 32b43f85bd a
2026-07-03 16:58:28 +08:00

134 lines
4.4 KiB
JavaScript

/**
* 微博登录助手 V2 - 直接访问登录页面
*/
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../config/browser');
const fs = require('fs');
const PHONE = process.argv[2] || '18688929976';
const SCREENSHOT_DIR = '/tmp/weibo_login';
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
if (!fs.existsSync(SCREENSHOT_DIR)) {
fs.mkdirSync(SCREENSHOT_DIR, { recursive: true });
}
async function weiboLogin() {
console.log('🌐 正在打开微博登录页面...');
const browser = await puppeteer.launch({
...getLaunchOptions(),
headless: true,
});
try {
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 900 });
// 直接访问登录页面
await page.goto('https://weibo.com/newlogin?tabtype=weibo', {
waitUntil: 'networkidle2',
timeout: 60000,
});
console.log('✅ 登录页面加载完成');
await delay(3000);
await page.screenshot({ path: `${SCREENSHOT_DIR}/login_page.png` });
console.log('📸 已截图');
// 查找并点击短信登录选项卡
const tabs = await page.$$('div[role="tab"], .tab, [class*="tab"]');
console.log(`找到 ${tabs.length} 个选项卡`);
for (const tab of tabs) {
const text = await page.evaluate(el => el.textContent, tab);
console.log(` 选项卡: "${text?.trim()}"`);
if (text && text.includes('短信')) {
console.log('🖱️ 点击短信登录选项卡');
await tab.click();
await delay(2000);
break;
}
}
await page.screenshot({ path: `${SCREENSHOT_DIR}/after_tab.png` });
// 查找手机号输入框
console.log('📱 查找手机号输入框...');
const phoneInput = await page.$('input[type="tel"], input[placeholder*="手机号"], input[name="phone"]');
if (phoneInput) {
await phoneInput.type(PHONE, { delay: 50 });
console.log(`✅ 手机号已输入: ${PHONE}`);
} else {
// 尝试所有输入框
const inputs = await page.$$('input');
console.log(`尝试 ${inputs.length} 个输入框...`);
for (const input of inputs) {
const placeholder = await page.evaluate(el => el.placeholder, input);
const type = await page.evaluate(el => el.type, input);
console.log(` input type=${type} placeholder="${placeholder}"`);
if (type === 'tel' || (placeholder && placeholder.includes('手机'))) {
await input.type(PHONE, { delay: 50 });
console.log(`✅ 手机号已输入: ${PHONE}`);
break;
}
}
}
await page.screenshot({ path: `${SCREENSHOT_DIR}/phone_entered.png` });
// 查找获取验证码按钮
console.log('🔍 查找"获取验证码"按钮...');
const buttons = await page.$$('button, a, div[role="button"]');
let codeBtn = null;
for (const btn of buttons) {
const text = await page.evaluate(el => el.textContent, btn);
if (text && (text.includes('获取') || text.includes('验证码') || text.includes('code'))) {
console.log(` 找到按钮: "${text.trim()}"`);
codeBtn = btn;
break;
}
}
if (codeBtn) {
console.log('🖱️ 点击获取验证码按钮...');
await codeBtn.click();
console.log('✅ 已发送验证码请求');
await delay(3000);
await page.screenshot({ path: `${SCREENSHOT_DIR}/code_requested.png` });
} else {
console.log('⚠️ 未找到验证码按钮');
}
console.log('\n' + '='.repeat(60));
console.log('📋 步骤完成,当前状态:');
console.log(` 手机号: ${PHONE}`);
console.log(` 验证码: ⏳ 等待发送...`);
console.log('\n📸 截图文件:');
fs.readdirSync(SCREENSHOT_DIR).forEach(f => {
console.log(` - ${SCREENSHOT_DIR}/${f}`);
});
console.log('\n📱 请检查手机 18688929976');
console.log('收到验证码后告诉我,格式: 验证码 123456');
console.log('='.repeat(60));
// 保存浏览器状态供后续使用
const cookies = await page.cookies();
fs.writeFileSync(`${SCREENSHOT_DIR}/cookies_step1.json`, JSON.stringify(cookies, null, 2));
// 保持运行等待验证码
await new Promise(() => {});
} catch (error) {
console.error('❌ 错误:', error.message);
await browser.close();
process.exit(1);
}
}
weiboLogin().catch(console.error);