141 lines
4.8 KiB
JavaScript
141 lines
4.8 KiB
JavaScript
/**
|
|
* 扫码登录后发送测试微博
|
|
*/
|
|
|
|
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));
|
|
|
|
async function postAfterQR() {
|
|
console.log('🌐 启动浏览器...');
|
|
|
|
const browser = await puppeteer.launch({
|
|
...getLaunchOptions(),
|
|
headless: true,
|
|
});
|
|
|
|
const page = await browser.newPage();
|
|
await page.setViewport({ width: 1280, height: 900 });
|
|
|
|
try {
|
|
// 访问微博主页验证登录
|
|
console.log('🔄 访问微博主页验证登录...');
|
|
await page.goto('https://weibo.com', { waitUntil: 'networkidle2', timeout: 60000 });
|
|
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}/logged_in_home.png` });
|
|
fs.copyFileSync(`${SCREENSHOT_DIR}/logged_in_home.png`, `/home/ubuntu/nanocode/audiobook-player/static/logged_in_home.png`);
|
|
|
|
// 检查是否已登录(看有没有"写微博"按钮)
|
|
const allBtns = await page.$$('button, a');
|
|
let hasWriteBtn = false;
|
|
for (const btn of allBtns) {
|
|
const text = await page.evaluate(el => el.textContent, btn);
|
|
if (text?.includes('写微博')) {
|
|
hasWriteBtn = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (hasWriteBtn) {
|
|
console.log('✅ 检测到"写微博"按钮,登录成功!');
|
|
} else {
|
|
console.log('⚠️ 未检测到"写微博"按钮,可能还在登录页');
|
|
}
|
|
|
|
// 尝试发送测试微博
|
|
console.log('\n📝 准备发送测试微博...');
|
|
const TEST_CONTENT = '🤖 二维码登录测试成功!时间:' + new Date().toLocaleString('zh-CN');
|
|
|
|
// 点击"写微博"
|
|
for (const btn of allBtns) {
|
|
const text = await page.evaluate(el => el.textContent, btn);
|
|
if (text?.includes('写微博')) {
|
|
console.log('🖱️ 点击"写微博"...');
|
|
await btn.click();
|
|
break;
|
|
}
|
|
}
|
|
|
|
await delay(2000);
|
|
await page.screenshot({ path: `${SCREENSHOT_DIR}/compose_opened.png` });
|
|
|
|
// 查找发布框
|
|
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);
|
|
await page.screenshot({ path: `${SCREENSHOT_DIR}/content_typed.png` });
|
|
fs.copyFileSync(`${SCREENSHOT_DIR}/content_typed.png`, `/home/ubuntu/nanocode/audiobook-player/static/content_typed.png`);
|
|
|
|
// 查找发送按钮
|
|
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;
|
|
console.log(`🖱️ 找到发送按钮: "${text.trim()}"`);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (sendBtn) {
|
|
// 检查按钮是否可用
|
|
const isEnabled = await page.evaluate(el => !el.disabled, sendBtn);
|
|
console.log(`📊 按钮状态: ${isEnabled ? '可用' : '禁用'}`);
|
|
|
|
console.log('🚀 点击发送...');
|
|
await sendBtn.click();
|
|
await delay(5000);
|
|
|
|
await page.screenshot({ path: `${SCREENSHOT_DIR}/after_send.png` });
|
|
fs.copyFileSync(`${SCREENSHOT_DIR}/after_send.png`, `/home/ubuntu/nanocode/audiobook-player/static/after_send.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.png` });
|
|
fs.copyFileSync(`${SCREENSHOT_DIR}/verify.png`, `/home/ubuntu/nanocode/audiobook-player/static/verify.png`);
|
|
|
|
console.log('📸 已截图验证页面');
|
|
console.log('\n🎉 测试微博发送流程已完成!');
|
|
|
|
} else {
|
|
console.log('⚠️ 未找到发送按钮');
|
|
}
|
|
} else {
|
|
console.log('⚠️ 未找到发布框');
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('❌ 错误:', error.message);
|
|
await page.screenshot({ path: `${SCREENSHOT_DIR}/error_post.png` });
|
|
fs.copyFileSync(`${SCREENSHOT_DIR}/error_post.png`, `/home/ubuntu/nanocode/audiobook-player/static/error_post.png`);
|
|
} finally {
|
|
await browser.close();
|
|
}
|
|
}
|
|
|
|
postAfterQR().catch(console.error);
|