150 lines
4.8 KiB
Python
150 lines
4.8 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""交行商户门户报表页 DOM 探测(只读,不点导出/不提交)。
|
||
|
||
复用抓包时的浏览器 profile(登录态若仍在则免扫码),进入交易查询页
|
||
#/MENU30060101,把筛选控件、按钮、菜单、iframe 结构导出到
|
||
logs/bankcomm_probe/dom.txt,用于写 UI 自动化选择器。
|
||
|
||
用法(仓库根目录):
|
||
.venv\\Scripts\\python.exe tests\\bankcomm_probe\\bankcomm_dom_probe.py
|
||
"""
|
||
import json
|
||
import os
|
||
import sys
|
||
import time
|
||
|
||
try:
|
||
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
|
||
except Exception:
|
||
pass
|
||
|
||
from DrissionPage import ChromiumPage, ChromiumOptions
|
||
|
||
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||
OUT_DIR = os.path.join(REPO_ROOT, 'logs', 'bankcomm_probe')
|
||
PROFILE_DIR = os.path.join(os.environ.get('LOCALAPPDATA') or OUT_DIR,
|
||
'37duAutomation', 'browser_sessions', 'bankcomm_probe')
|
||
|
||
SITE_URL = 'https://merchantapp.bankcomm.com/mm/'
|
||
REPORT_HASH = '#/MENU30060101'
|
||
LOCAL_PORT = 9333
|
||
SCAN_WAIT = 300
|
||
|
||
DUMP_JS = r"""
|
||
() => {
|
||
const labelOf = (el) => {
|
||
let p = el.closest('.ant-form-item, .ant-row, li, td, div');
|
||
for (let i = 0; i < 4 && p; i++) {
|
||
const l = p.querySelector('label, .ant-form-item-label, .label, .title');
|
||
if (l && l.innerText && l.innerText.trim()) return l.innerText.trim().slice(0, 30);
|
||
p = p.parentElement;
|
||
}
|
||
return '';
|
||
};
|
||
const els = [];
|
||
document.querySelectorAll('input, textarea, select, button, [role="combobox"]').forEach(el => {
|
||
const r = el.getBoundingClientRect();
|
||
els.push({
|
||
tag: el.tagName,
|
||
type: el.type || '',
|
||
cls: String(el.className || '').slice(0, 120),
|
||
ph: el.placeholder || '',
|
||
val: String(el.value || '').slice(0, 40),
|
||
readOnly: !!el.readOnly,
|
||
label: labelOf(el),
|
||
txt: (el.innerText || '').trim().slice(0, 40),
|
||
box: [Math.round(r.x), Math.round(r.y), Math.round(r.width), Math.round(r.height)],
|
||
visible: !!(el.offsetWidth || el.offsetHeight)
|
||
});
|
||
});
|
||
const iframes = [...document.querySelectorAll('iframe')].map(f => ({src: f.src, w: f.offsetWidth, h: f.offsetHeight}));
|
||
const menus = [...document.querySelectorAll('a, li, .ant-menu-item, .ant-menu-submenu-title')]
|
||
.map(e => (e.innerText || '').trim())
|
||
.filter(t => t && t.length <= 20).slice(0, 120);
|
||
let storeKeys = {};
|
||
try {
|
||
for (const s of ['localStorage', 'sessionStorage']) {
|
||
const st = window[s]; const ks = [];
|
||
for (let i = 0; i < st.length; i++) ks.push(st.key(i));
|
||
storeKeys[s] = ks;
|
||
}
|
||
} catch (e) { storeKeys = String(e); }
|
||
return JSON.stringify({
|
||
url: location.href,
|
||
title: document.title,
|
||
els: els,
|
||
iframes: iframes,
|
||
menus: menus,
|
||
storeKeys: storeKeys,
|
||
text: (document.body.innerText || '').slice(0, 6000)
|
||
});
|
||
}
|
||
"""
|
||
|
||
|
||
def log(msg):
|
||
print(f'[{time.strftime("%H:%M:%S")}] {msg}', flush=True)
|
||
|
||
|
||
def main():
|
||
os.makedirs(OUT_DIR, exist_ok=True)
|
||
co = ChromiumOptions()
|
||
co.set_local_port(LOCAL_PORT)
|
||
co.set_user_data_path(PROFILE_DIR)
|
||
co.set_argument('--start-maximized')
|
||
page = ChromiumPage(co)
|
||
page.get(SITE_URL)
|
||
time.sleep(3)
|
||
|
||
log(f'当前 URL:{page.url}')
|
||
|
||
# 登录态失效则等扫码
|
||
deadline = time.time() + SCAN_WAIT
|
||
hinted = False
|
||
while time.time() < deadline:
|
||
if 'login' not in page.url.lower():
|
||
break
|
||
if not hinted:
|
||
log('登录态已失效:请在弹出的浏览器窗口里扫码登录(脚本最多等 5 分钟)')
|
||
hinted = True
|
||
time.sleep(2)
|
||
if 'login' in page.url.lower():
|
||
log('等待扫码超时,退出')
|
||
try:
|
||
page.quit()
|
||
except Exception:
|
||
pass
|
||
return 1
|
||
log(f'已登录:{page.url}')
|
||
|
||
# 进交易查询页(改 hash 触发路由)
|
||
page.get(SITE_URL + REPORT_HASH)
|
||
time.sleep(6)
|
||
log(f'报表页 URL:{page.url}')
|
||
|
||
data = page.run_js(DUMP_JS)
|
||
try:
|
||
info = json.loads(data)
|
||
except Exception:
|
||
info = {'raw': str(data)[:5000]}
|
||
|
||
out = os.path.join(OUT_DIR, 'dom.txt')
|
||
with open(out, 'w', encoding='utf-8') as f:
|
||
f.write(json.dumps(info, ensure_ascii=False, indent=2))
|
||
log(f'DOM 已导出:{out}(控件 {len(info.get("els") or [])} 个,iframe {len(info.get("iframes") or [])} 个)')
|
||
|
||
# 截图留档
|
||
try:
|
||
shot = os.path.join(OUT_DIR, 'report_page.png')
|
||
page.get_screenshot(path=OUT_DIR, name='report_page')
|
||
log(f'页面截图:{shot}')
|
||
except Exception as e:
|
||
log(f'截图失败:{e}')
|
||
|
||
log('浏览器保持打开,可继续人工查看;关闭窗口即结束')
|
||
return 0
|
||
|
||
|
||
if __name__ == '__main__':
|
||
sys.exit(main())
|