const puppeteer = require('puppeteer-core'); const { getLaunchOptions } = require('../config/browser'); const fs = require('fs'); const delay = (ms) => new Promise(r => setTimeout(r, ms)); const TEST_CONTENT = '🤖 二维码登录测试成功!时间:' + new Date().toLocaleString('zh-CN'); (async () => { const browser = await puppeteer.launch(getLaunchOptions()); const page = await browser.newPage(); await page.setViewport({ width: 1280, height: 900 }); // 访问主页 await page.goto('https://weibo.com', { waitUntil: 'networkidle2' }); await delay(3000); console.log('🔗 URL:', page.url()); console.log('📄 标题:', await page.title()); // 找写微博按钮并点击 const writeBtn = await page.evaluateHandle(() => { const btns = Array.from(document.querySelectorAll('button, a, div[role="button"]')); return btns.find(b => b.textContent && b.textContent.includes('写微博')); }); if (writeBtn) { console.log('🖱️ 点击写微博...'); await writeBtn.click(); await delay(2000); // 找发布框 const postBox = await page.$('textarea[placeholder*="有什么新鲜事"]'); if (postBox) { await postBox.click(); await delay(500); await postBox.type(TEST_CONTENT, { delay: 30 }); console.log('✍️ 已输入:', TEST_CONTENT); await delay(1000); // 找发送按钮 const sendBtn = await page.evaluateHandle(() => { const btns = Array.from(document.querySelectorAll('button, a, div[role="button"]')); return btns.find(b => b.textContent && b.textContent.includes('发送')); }); if (sendBtn) { console.log('🚀 点击发送...'); await sendBtn.click(); await delay(5000); console.log('✅ 已发送!'); await page.screenshot({ path: '/tmp/weibo_qr_login/final_result.png' }); fs.copyFileSync('/tmp/weibo_qr_login/final_result.png', '/home/ubuntu/nanocode/audiobook-player/static/final_result.png'); // 验证 await page.goto('https://weibo.com/u/me?tabtype=weibo', { waitUntil: 'networkidle2' }); await delay(3000); await page.screenshot({ path: '/tmp/weibo_qr_login/verify_post.png' }); fs.copyFileSync('/tmp/weibo_qr_login/verify_post.png', '/home/ubuntu/nanocode/audiobook-player/static/verify_post.png'); console.log('📸 验证截图已保存'); console.log('🎉 完成!'); } else { console.log('⚠️ 未找到发送按钮'); } } else { console.log('⚠️ 未找到发布框'); } } else { console.log('⚠️ 未找到写微博按钮'); } await browser.close(); })();