@@ -39,46 +39,52 @@ jobs:
3939 run : |
4040 node -e "const puppeteer = require('puppeteer');
4141 (async () => {
42- const browser = await puppeteer.launch({
43- args: ['--no-sandbox', '--disable-setuid-sandbox']
44- });
45- const page = await browser.newPage();
46-
47- // 设置更大的视口以显示更多内容
48- await page.setViewport({width: 1280, height: 960});
49-
50- // 访问页面
51- await page.goto('https://huggingface.co/spaces/huggingface/ai-deadlines', {
52- waitUntil: 'networkidle0', // 等待网络请求完成
53- timeout: 60000 // 等待最多60秒
54- });
55-
56- // 等待页面内容加载
57- await page.waitForSelector('main', {timeout: 30000}).catch(() => console.log('正在等待内容加载...'));
58-
59- // 额外等待时间让JavaScript完全执行
60- await page.waitForTimeout(5000);
61-
62- // 确保 Images 目录存在
63- await require('fs').promises.mkdir('./Images', { recursive: true }).catch(() => {});
64-
65- // 根据XPath找到元素
66- const mainElement = await page.$x('/html/body/div[1]/div[2]/main');
67- if (mainElement.length > 0) {
68- // 只截取main元素部分
69- await mainElement[0].screenshot({
70- path: './Images/DDL.png'
42+ try {
43+ console.log('启动浏览器...');
44+ const browser = await puppeteer.launch({
45+ args: ['--no-sandbox', '--disable-setuid-sandbox']
7146 });
72- console.log('成功截取main元素');
73- } else {
74- // 如果找不到元素,退回到全页截图
75- console.log('无法找到指定元素,使用全页截图');
76- await page.screenshot({path: './Images/DDL.png', fullPage: true});
47+ const page = await browser.newPage();
48+
49+ // 设置更大的视口以显示更多内容
50+ await page.setViewport({width: 1280, height: 960});
51+
52+ console.log('开始加载页面...');
53+ // 使用更宽松的等待条件,提高超时时间
54+ await page.goto('https://huggingface.co/spaces/huggingface/ai-deadlines', {
55+ waitUntil: 'domcontentloaded', // 更宽松的等待条件
56+ timeout: 120000 // 增加到120秒
57+ });
58+ console.log('页面基本加载完成');
59+
60+ // 确保 Images 目录存在
61+ await require('fs').promises.mkdir('./Images', { recursive: true }).catch(() => {});
62+
63+ // 先截取当前可见内容(即使不完整)
64+ await page.screenshot({path: './Images/DDL.png'});
65+ console.log('已截取初始页面');
66+
67+ // 尝试等待更多内容加载
68+ try {
69+ await page.waitForSelector('main', {timeout: 15000});
70+ console.log('main元素已加载');
71+ await page.waitForTimeout(3000);
72+
73+ // 截取更新后的页面
74+ await page.screenshot({path: './Images/DDL.png', fullPage: true});
75+ console.log('已截取完整页面');
76+ } catch (innerErr) {
77+ console.log('等待元素超时,使用已有截图: ' + innerErr.message);
78+ }
79+
80+ await browser.close();
81+ console.log('浏览器已关闭');
82+ } catch (err) {
83+ console.error('发生错误: ' + err.message);
84+ // 确保即使出错也返回成功,这样工作流可以继续
85+ process.exit(0);
7786 }
78-
79- await browser.close();
80- })();" # 使用 Node.js 脚本执行 Puppeteer 代码
81-
87+ })();"
8288 - name : Push changes
8389 run : |
8490 git config --global user.name 'github-actions'
0 commit comments