76 lines
2.7 KiB
JavaScript
76 lines
2.7 KiB
JavaScript
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 result = await page.evaluate(async (content) => {
|
|
// 找写微博按钮
|
|
const writeBtn = Array.from(document.querySelectorAll('button, a, div[role="button"]'))
|
|
.find(b => b.textContent && b.textContent.includes('写微博'));
|
|
|
|
if (!writeBtn) return { error: '未找到写微博按钮' };
|
|
|
|
// 点击
|
|
writeBtn.click();
|
|
await new Promise(r => setTimeout(r, 2000));
|
|
|
|
// 找发布框
|
|
const postBox = document.querySelector('textarea[placeholder*="有什么新鲜事"]') ||
|
|
document.querySelector('textarea');
|
|
|
|
if (!postBox) return { error: '未找到发布框' };
|
|
|
|
// 输入内容
|
|
postBox.click();
|
|
postBox.value = content;
|
|
postBox.dispatchEvent(new Event('input', { bubbles: true }));
|
|
|
|
await new Promise(r => setTimeout(r, 1000));
|
|
|
|
// 找发送按钮
|
|
const sendBtn = Array.from(document.querySelectorAll('button, a, div[role="button"]'))
|
|
.find(b => b.textContent && b.textContent.includes('发送') && !b.disabled);
|
|
|
|
if (!sendBtn) return { error: '未找到发送按钮' };
|
|
|
|
// 点击发送
|
|
sendBtn.click();
|
|
|
|
return { success: true, message: '已点击发送' };
|
|
}, TEST_CONTENT);
|
|
|
|
console.log('📝 执行结果:', result);
|
|
|
|
if (result.success) {
|
|
await delay(5000);
|
|
await page.screenshot({ path: '/tmp/weibo_qr_login/after_send_js.png' });
|
|
fs.copyFileSync('/tmp/weibo_qr_login/after_send_js.png', '/home/ubuntu/nanocode/audiobook-player/static/after_send_js.png');
|
|
|
|
// 验证
|
|
await page.goto('https://weibo.com/u/me?tabtype=weibo', { waitUntil: 'networkidle2' });
|
|
await delay(3000);
|
|
await page.screenshot({ path: '/tmp/weibo_qr_login/final_verify.png' });
|
|
fs.copyFileSync('/tmp/weibo_qr_login/final_verify.png', '/home/ubuntu/nanocode/audiobook-player/static/final_verify.png');
|
|
|
|
console.log('✅ 流程完成!');
|
|
console.log('📸 截图: http://129.211.173.87:8899/screenshots/final_verify.png');
|
|
}
|
|
|
|
await browser.close();
|
|
})();
|