Files

188 lines
6.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""实测「交易汇总」页 6 步链路(跑到查询为止,不点导出)。
步骤:门店全选 → 自定义日期 → 日历面板选日期 → 查询 → 查看详情(观察是否开新页)
"""
import json
import os
import sys
import time
try:
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
except Exception:
pass
from DrissionPage import ChromiumPage
REPO = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
SUMMARY_URL = 'https://merchantapp.bankcomm.com/mm/#/MENU30060301'
D0, D1 = '2026年10月31日', '2026年10月31日'
def log(m):
print(f'[{time.strftime("%H:%M:%S")}] {m}', flush=True)
JS_MARK = r"""
var t = arguments[0];
var p = document.querySelector('.ant-calendar-picker-container');
if (!p || !p.offsetWidth) return 'no-panel';
var tds = [].slice.call(p.querySelectorAll('td[title]'));
for (var i = 0; i < tds.length; i++) {
if ((tds[i].getAttribute('title') || '') === t) {
var old = document.getElementById('__pick_day__');
if (old) old.id = '';
tds[i].id = '__pick_day__';
return 'ok';
}
}
return 'not-found';
"""
JS_STEP = r"""
var dir = arguments[0];
var p = document.querySelector('.ant-calendar-picker-container');
if (!p) return false;
var b = dir === 'next' ? p.querySelector('.ant-calendar-next-month-btn')
: p.querySelector('.ant-calendar-prev-month-btn');
if (b) { b.click(); return true; }
return false;
"""
page = ChromiumPage('127.0.0.1:9333')
page.get(SUMMARY_URL)
time.sleep(6)
log(f'URL {page.url}')
# ---------- ① 门店全选 ----------
log('① 打开门店多选…')
multi = page.ele('css:.ant-select-selection--multiple', timeout=10)
if not multi:
log('找不到门店多选框')
sys.exit(1)
multi.click()
time.sleep(1.2)
checked = False
for wrap in page.eles('css:.ant-select-dropdown .ant-checkbox-wrapper'):
try:
if '全选' in (wrap.text or ''):
inp = wrap.ele('css:input.ant-checkbox-input')
checked_now = False
try:
checked_now = bool(inp.states.is_checked)
except Exception:
pass
if not checked_now:
inp.click()
checked = True
log(f'已点「全选」(之前勾选={checked_now})')
break
except Exception as e:
log(f'全选异常 {e}')
time.sleep(1)
# 收起下拉
try:
page.actions.click((900, 120))
except Exception:
pass
time.sleep(1)
sel_text = page.run_js(
"var e=document.querySelector('.ant-select-selection--multiple');"
"return e?(e.innerText||'').replace(/\\s+/g,' ').slice(0,80):''")
log(f'门店选择结果:{sel_text}')
# ---------- ② 自定义日期 ----------
log('② 点「自定义日期」…')
ok = False
for b in page.eles('tag:button'):
try:
if b.states.is_displayed and '自定义日期' in ''.join((b.text or '').split()):
b.click()
ok = True
break
except Exception:
continue
log(f'点击={ok}')
time.sleep(1.5)
dates = page.run_js("""
var a=[].slice.call(document.querySelectorAll('input.ant-calendar-picker-input'));
return JSON.stringify(a.map(function(e){var r=e.getBoundingClientRect();return {v:e.value,w:Math.round(r.width),ro:e.readOnly};}));
""")
log(f'日期控件:{dates}')
# ---------- ③ 日历面板选日期 ----------
def pick(index, title):
boxes = [e for e in page.eles('css:input.ant-calendar-picker-input') if e.states.is_displayed]
if len(boxes) <= index:
return False, f'只有 {len(boxes)} 个日期框'
boxes[index].click()
time.sleep(1.0)
mark = page.run_js(JS_MARK, title)
steps = 0
while mark == 'not-found' and steps < 30:
page.run_js(JS_STEP, 'prev')
time.sleep(0.35)
steps += 1
mark = page.run_js(JS_MARK, title)
if mark != 'ok':
return False, f'面板找不到 {title}(翻 {steps} 次)'
el = page.ele('#__pick_day__', timeout=3)
if not el:
return False, '格子取不到'
el.click()
time.sleep(0.8)
page.run_js("var e=document.getElementById('__pick_day__'); if(e) e.id='';")
return True, f'翻月 {steps} 次'
log('③ 选开始日期…')
log(str(pick(0, D0)))
log('③ 选结束日期…')
log(str(pick(1, D1)))
log('日期结果:' + page.run_js("""
var a=[].slice.call(document.querySelectorAll('input.ant-calendar-picker-input'));
return JSON.stringify(a.map(function(e){return e.value;}));
"""))
# ---------- ④ 查询 ----------
log('④ 点「查询」…')
before = page.run_js("var e=document.querySelector('.ant-calendar-picker-input'); return document.body.innerText.match(/当前统计截止到[^\\n]*/)?.[0]||'';")
for b in page.eles('tag:button'):
try:
if b.states.is_displayed and ''.join((b.text or '').split()) == '查询':
b.click()
log('已点查询')
break
except Exception:
continue
for i in range(30):
time.sleep(2)
txt = page.run_js("return document.body.innerText.match(/当前统计截止到[^\\n]*/)?.[0]||'';")
if txt and txt != before:
log(f'结果已刷新:{txt}(原 {before})')
break
if i % 3 == 0:
log(f' 等待…{txt}')
# ---------- ⑤ 查看详情 ----------
log('⑤ 点「查看详情」…')
tabs_before = set(page.tab_ids)
for b in page.eles('css:button.onClickShowDetail'):
try:
if b.states.is_displayed:
b.click()
log('已点第一个「查看详情」')
break
except Exception as e:
log(f'点击异常 {e}')
time.sleep(5)
new_tabs = set(page.tab_ids) - tabs_before
log(f'新标签:{new_tabs};当前 URL:{page.url}')
if new_tabs:
t = page.get_tab(list(new_tabs)[0])
log(f'新标签 URL:{t.url}')
log('新标签正文:' + (t.run_js('return document.body.innerText') or '')[:400])
else:
log('同页跳转,正文:' + (page.run_js('return document.body.innerText') or '')[:400])