184 lines
5.2 KiB
JavaScript
184 lines
5.2 KiB
JavaScript
/**
|
|
* 微博二维码登录测试
|
|
* 1. 打开登录页,截取二维码
|
|
* 2. 等待用户扫码
|
|
* 3. 验证登录并发微博
|
|
*/
|
|
|
|
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/newlogin?tabtype=weibo', {
|
|
waitUntil: 'networkidle2',
|
|
timeout: 60000,
|
|
});
|
|
|
|
await delay(3000);
|
|
console.log('✅ 登录页加载完成');
|
|
|
|
// 截图 - 应该包含二维码
|
|
const qrScreenshot = `${SCREENSHOT_DIR}/qr_code.png`;
|
|
await page.screenshot({ path: qrScreenshot });
|
|
console.log(`📸 已截图: ${qrScreenshot}`);
|
|
|
|
// 检查页面是否有二维码元素
|
|
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;
|
|
}
|
|
}
|
|
|
|
if (!qrFound) {
|
|
console.log('⚠️ 未明显找到二维码元素,但已截图整页');
|
|
}
|
|
|
|
console.log('\n' + '='.repeat(60));
|
|
console.log('📧 请查收邮件中的二维码截图');
|
|
console.log('📱 请使用微博 App 扫描二维码');
|
|
console.log('⏳ 等待 60 秒让您扫码...');
|
|
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('✅ 登录成功!');
|
|
|
|
// 尝试发微博
|
|
console.log('\n📝 尝试发送测试微博...');
|
|
await postTestWeibo(page);
|
|
|
|
} else {
|
|
console.log('⏳ 可能还未扫码或扫码未完成');
|
|
console.log('💡 如果需要更多时间,请告诉我延长等待');
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('❌ 错误:', error.message);
|
|
await page.screenshot({ path: `${SCREENSHOT_DIR}/error.png` });
|
|
} 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('✅ 发送完成!');
|
|
|
|
// 验证
|
|
await page.goto('https://weibo.com/u/me?tabtype=weibo', { waitUntil: 'networkidle2' });
|
|
await delay(3000);
|
|
await page.screenshot({ path: `${SCREENSHOT_DIR}/verify_post.png` });
|
|
console.log('📸 已截图验证页面');
|
|
|
|
} else {
|
|
console.log('⚠️ 未找到发送按钮');
|
|
}
|
|
} else {
|
|
console.log('⚠️ 未找到发布框');
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('❌ 发微博失败:', error.message);
|
|
}
|
|
}
|
|
|
|
weiboQRLogin().catch(console.error);
|