diff --git a/scripts/erp-daily-bills/export-daily.mjs b/scripts/erp-daily-bills/export-daily.mjs index 2f39496e..97c72db0 100644 --- a/scripts/erp-daily-bills/export-daily.mjs +++ b/scripts/erp-daily-bills/export-daily.mjs @@ -111,6 +111,44 @@ async function closeAuthModal(cdp, sid, rounds = 3) { if (hidden) log('授权过期弹窗卡在关闭动画,已隐藏遮挡元素 ' + hidden + ' 个'); } +async function selectTabByText(cdp, sid, { selector, text, active }) { + for (let attempt = 1; attempt <= 15; attempt++) { + await closeAuthModal(cdp, sid, 2); + const item = await evaluate(cdp, sid, `(() => { + const visible = (el) => (el.offsetWidth || el.offsetHeight) && getComputedStyle(el).display !== 'none'; + const norm = (value) => (value || '').replace(/\\s+/g, ''); + const el = [...document.querySelectorAll(${JSON.stringify(selector)})] + .find((candidate) => visible(candidate) && norm(candidate.innerText) === ${JSON.stringify(text)}); + if (!el) return null; + el.scrollIntoView({ block: 'nearest', inline: 'center' }); + const rect = el.getBoundingClientRect(); + return { + active: ${active}, + x: rect.left + rect.width / 2, + y: rect.top + rect.height / 2, + }; + })()`); + if (!item) { + if ([1, 5, 10, 15].includes(attempt)) log(` 未找到页面选项「${text}」,第 ${attempt} 次`); + await sleep(1500); + continue; + } + if (item.active) return; + await clickPoint(cdp, sid, item.x, item.y); + await sleep(2000); + const selected = await evaluate(cdp, sid, `(() => { + const visible = (el) => (el.offsetWidth || el.offsetHeight) && getComputedStyle(el).display !== 'none'; + const norm = (value) => (value || '').replace(/\\s+/g, ''); + const el = [...document.querySelectorAll(${JSON.stringify(selector)})] + .find((candidate) => visible(candidate) && norm(candidate.innerText) === ${JSON.stringify(text)}); + return !!el && (${active}); + })()`); + if (selected) return; + log(` 页面选项「${text}」未生效,第 ${attempt} 次重试`); + } + throw new Error('页面选项未能切换:' + text); +} + const { cdp, sessionId, state, close } = await loginErp({ headless: true }); if (!/epaas/.test(state.url)) { await goto(cdp, sessionId, 'https://ww.erp321.com/epaas'); @@ -143,10 +181,18 @@ await cdp.send('Runtime.enable', {}, sid); await goto(cdp, sid, BILL_URL); await sleep(6000); await closeAuthModal(cdp, sid); -await evaluate(cdp, sid, `(() => { const norm=(s)=>(s||'').replace(/\\s+/g,''); const el=[...document.querySelectorAll('.ant-tabs-tab-btn, [role=tab]')].find(e=>norm(e.innerText)===${JSON.stringify(platform.label)}); el && el.click(); })()`); +await selectTabByText(cdp, sid, { + selector: '.ant-tabs-tab-btn, [role=tab]', + text: platform.label, + active: "el.getAttribute('aria-selected') === 'true'", +}); await sleep(4500); await closeAuthModal(cdp, sid); -await evaluate(cdp, sid, `(() => { const norm=(s)=>(s||'').replace(/\\s+/g,''); const el=[...document.querySelectorAll('.sub-tab-item')].find(e=>norm(e.innerText)===${JSON.stringify(platform.subTab)}); el && el.click(); })()`); +await selectTabByText(cdp, sid, { + selector: '.sub-tab-item', + text: platform.subTab, + active: "(el.className || '').includes('active')", +}); await sleep(2500); await closeAuthModal(cdp, sid);