211 lines
6.1 KiB
JavaScript
211 lines
6.1 KiB
JavaScript
/**
|
|
* 微博二维码登录测试 V2
|
|
* 1. 访问微博首页
|
|
* 2. 点击"登录/注册"按钮
|
|
* 3. 截取二维码
|
|
* 4. 等待扫码并验证
|
|
*/
|
|
|
|
const puppeteer = require('puppeteer-core');
|
|
const { getLaunchOptions } = require('../config/browser');
|
|
const fs = require('fs');
|
|
|
|
const SCREENSHOT_DIR = '/tmp/weibo_qr_login';
|
|
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
if (!fs.existsSync(SCREENSHOT_DIR)) {
|
|
fs.mkdirSync(SCREENSHOT_DIR, { recursive: true });
|
|
}
|
|
|
|
async function weiboQRLogin() {
|
|
console.log('🌐 启动浏览器...');
|
|
|
|
const browser = await puppeteer.launch({
|
|
...getLaunchOptions(),
|
|
headless: true,
|
|
});
|
|
|
|
try {
|
|
const page = await browser.newPage();
|
|
await page.setViewport({ width: 1280, height: 900 });
|
|
|
|
// 访问微博首页
|
|
console.log('🔄 访问微博首页...');
|
|
await page.goto('https://weibo.com', {
|
|
waitUntil: 'networkidle2',
|
|
timeout: 60000,
|
|
});
|
|
|
|
await delay(3000);
|
|
console.log('✅ 首页加载完成');
|
|
|
|
// 截图 - 首页状态
|
|
await page.screenshot({ path: `${SCREENSHOT_DIR}/homepage.png` });
|
|
|
|
// 查找并点击"登录"按钮
|
|
console.log('🔍 查找登录按钮...');
|
|
let loginBtn = null;
|
|
|
|
// 通过文本查找
|
|
const allLinks = await page.$$('a, button, div[role="button"]');
|
|
for (const link of allLinks) {
|
|
const text = await page.evaluate(el => el.textContent, link);
|
|
if (text && (text.trim() === '登录' || text.includes('登录/注册'))) {
|
|
console.log(`✅ 找到登录按钮: "${text.trim()}"`);
|
|
loginBtn = link;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!loginBtn) {
|
|
console.log('⚠️ 通过文本未找到,尝试选择器...');
|
|
loginBtn = await page.$('a[href*="login"], .login, [node-type="loginBtn"]');
|
|
}
|
|
|
|
if (loginBtn) {
|
|
console.log('🖱️ 点击登录按钮...');
|
|
try {
|
|
await loginBtn.click();
|
|
} catch (e) {
|
|
// 如果点击失败,尝试 JavaScript 点击
|
|
console.log(' 普通点击失败,尝试 JS 点击...');
|
|
await page.evaluate(el => el.click(), loginBtn);
|
|
}
|
|
await delay(3000);
|
|
console.log('✅ 已点击登录');
|
|
} else {
|
|
console.log('❌ 未找到登录按钮,尝试直接访问登录链接');
|
|
await page.goto('https://weibo.com/newlogin', { waitUntil: 'networkidle2' });
|
|
await delay(3000);
|
|
}
|
|
|
|
// 截图 - 应该显示登录弹窗/页面
|
|
await page.screenshot({ path: `${SCREENSHOT_DIR}/login_page.png` });
|
|
console.log('📸 已截图登录页面');
|
|
|
|
// 检查是否有二维码
|
|
const qrSelectors = [
|
|
'img[src*="qr"]',
|
|
'[class*="qr"]',
|
|
'[class*="qrcode"]',
|
|
'canvas',
|
|
];
|
|
|
|
let qrFound = false;
|
|
for (const sel of qrSelectors) {
|
|
const el = await page.$(sel);
|
|
if (el) {
|
|
console.log(`✅ 找到二维码元素: ${sel}`);
|
|
qrFound = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 专门截图二维码区域
|
|
const qrScreenshot = `${SCREENSHOT_DIR}/qr_code_v2.png`;
|
|
await page.screenshot({ path: qrScreenshot });
|
|
console.log(`📸 已保存: ${qrScreenshot}`);
|
|
|
|
console.log('\n' + '='.repeat(60));
|
|
console.log('📧 请查看登录页面截图');
|
|
console.log('📱 请使用微博 App 扫描二维码');
|
|
console.log('⏳ 等待扫码...');
|
|
console.log('='.repeat(60) + '\n');
|
|
|
|
// 等待用户扫码
|
|
await delay(60000);
|
|
|
|
// 检查登录状态
|
|
console.log('🔍 检查登录状态...');
|
|
await page.reload({ waitUntil: 'networkidle2' });
|
|
await delay(3000);
|
|
|
|
const currentUrl = page.url();
|
|
const title = await page.title();
|
|
console.log(`🔗 当前URL: ${currentUrl}`);
|
|
console.log(`📄 页面标题: ${title}`);
|
|
|
|
await page.screenshot({ path: `${SCREENSHOT_DIR}/after_scan.png` });
|
|
|
|
// 判断是否登录成功
|
|
const isLoggedIn = !currentUrl.includes('login') && !title.includes('登录');
|
|
|
|
if (isLoggedIn) {
|
|
console.log('✅ 登录成功!');
|
|
// 尝试发微博
|
|
await postTestWeibo(page);
|
|
} else {
|
|
console.log('⏳ 可能还未扫码完成');
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('❌ 错误:', error.message);
|
|
try {
|
|
if (typeof page !== 'undefined') {
|
|
await page.screenshot({ path: `${SCREENSHOT_DIR}/error.png` });
|
|
}
|
|
} catch(e) {}
|
|
} finally {
|
|
await browser.close();
|
|
}
|
|
}
|
|
|
|
async function postTestWeibo(page) {
|
|
const TEST_CONTENT = '🤖 二维码登录测试 ' + new Date().toLocaleString('zh-CN');
|
|
|
|
try {
|
|
// 点击"写微博"
|
|
const allBtns = await page.$$('button, a, div[role="button"]');
|
|
for (const btn of allBtns) {
|
|
const text = await page.evaluate(el => el.textContent, btn);
|
|
if (text?.includes('写微博')) {
|
|
await btn.click();
|
|
console.log('🖱️ 点击"写微博"');
|
|
break;
|
|
}
|
|
}
|
|
|
|
await delay(2000);
|
|
|
|
// 查找发布框
|
|
let postBox = await page.$('textarea[placeholder*="有什么新鲜事"]');
|
|
if (!postBox) {
|
|
const textareas = await page.$$('textarea');
|
|
if (textareas.length > 0) postBox = textareas[0];
|
|
}
|
|
|
|
if (postBox) {
|
|
await postBox.click();
|
|
await delay(500);
|
|
await postBox.type(TEST_CONTENT, { delay: 30 });
|
|
console.log('✍️ 已输入:', TEST_CONTENT);
|
|
|
|
await delay(1000);
|
|
|
|
// 查找发送按钮
|
|
let sendBtn = null;
|
|
const allBtns2 = await page.$$('button, a, div[role="button"]');
|
|
for (const btn of allBtns2) {
|
|
const text = await page.evaluate(el => el.textContent, btn);
|
|
if (text?.includes('发送')) {
|
|
sendBtn = btn;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (sendBtn) {
|
|
console.log('🚀 点击发送...');
|
|
await sendBtn.click();
|
|
await delay(5000);
|
|
|
|
await page.screenshot({ path: `${SCREENSHOT_DIR}/post_result.png` });
|
|
console.log('✅ 发送完成!');
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('❌ 发微博失败:', error.message);
|
|
}
|
|
}
|
|
|
|
weiboQRLogin().catch(console.error);
|