feat: 新增交通银行数据导出插件及测试通道机制
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""xq0076 数据层公共工具(按钮定位 / 登录页判定 / 点击等待)。
|
||||
|
||||
交行页面是 Ant Design,按钮文本常带中文空格("登 录" / "导 出" / "跳 过"),
|
||||
且 xpath 中文匹配易翻车,统一改为「遍历 tag:button + 去空格文本比对」。
|
||||
"""
|
||||
import time
|
||||
|
||||
from .. import config
|
||||
|
||||
|
||||
def norm(text):
|
||||
"""去所有空白,便于「登 录」==「登录」比较。"""
|
||||
return ''.join(str(text or '').split())
|
||||
|
||||
|
||||
def is_login_page(page):
|
||||
url = (getattr(page, 'url', '') or '').lower()
|
||||
return config.LOGIN_HASH in url
|
||||
|
||||
|
||||
def is_displayed(el):
|
||||
"""真实可见性判断。
|
||||
|
||||
不能用 el.states.is_displayed 一个条件:交行把「图形验证码」放在
|
||||
`<div style="display:none">` 里,DP 仍判 True,导致误报"检测到验证码"。
|
||||
这里追加「渲染尺寸 > 0」校验(隐藏元素 rect 为 0 或取不到)。
|
||||
"""
|
||||
try:
|
||||
if not el.states.is_displayed:
|
||||
return False
|
||||
except Exception:
|
||||
return False
|
||||
try:
|
||||
w, h = el.rect.size
|
||||
return w > 0 and h > 0
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def find_button(page, text, timeout=8, contains=False, only_enabled=True):
|
||||
"""按去空格后的文本找按钮;找不到返回 None。
|
||||
|
||||
contains=True 时按包含匹配("同意" 命中 "我已阅读并同意")。
|
||||
"""
|
||||
target = norm(text)
|
||||
deadline = time.time() + timeout
|
||||
while True:
|
||||
try:
|
||||
for b in page.eles('tag:button'):
|
||||
try:
|
||||
if not is_displayed(b):
|
||||
continue
|
||||
label = norm(b.text)
|
||||
hit = (target in label) if contains else (label == target)
|
||||
if not hit:
|
||||
continue
|
||||
if only_enabled:
|
||||
try:
|
||||
if b.states.is_enabled is False:
|
||||
continue
|
||||
except Exception:
|
||||
pass
|
||||
return b
|
||||
except Exception:
|
||||
continue
|
||||
except Exception:
|
||||
pass
|
||||
if time.time() >= deadline:
|
||||
return None
|
||||
time.sleep(0.5)
|
||||
|
||||
|
||||
def click_button(page, text, timeout=8, contains=False):
|
||||
btn = find_button(page, text, timeout=timeout, contains=contains)
|
||||
if not btn:
|
||||
return False
|
||||
try:
|
||||
btn.click()
|
||||
return True
|
||||
except Exception as e:
|
||||
config.log(f'点击「{text}」失败:{e}', 'WARN')
|
||||
return False
|
||||
|
||||
|
||||
def scroll_to_bottom(page, settle=2.0, fast=False):
|
||||
"""滚动到页面底部(政策页必须滚到底,同意按钮才可点)。
|
||||
|
||||
⚠️ 不用「瞬间 scrollTop = scrollHeight」:位置跳变太假,容易被风控识别。
|
||||
默认走浏览器原生平滑滚动(behavior='smooth'),再停留 settle 秒,
|
||||
观感与人工拖动一致;内部可滚动容器同样平滑滚动。
|
||||
fast=True 时退回直接置底(仅用于非登录场景)。
|
||||
"""
|
||||
if fast:
|
||||
try:
|
||||
page.scroll.to_bottom()
|
||||
except Exception:
|
||||
pass
|
||||
time.sleep(0.2)
|
||||
return True
|
||||
|
||||
ok = False
|
||||
try:
|
||||
page.run_js("""
|
||||
var behavior = 'smooth';
|
||||
try { window.scrollTo({ top: document.body.scrollHeight, behavior: behavior }); }
|
||||
catch (e) { window.scrollTo(0, document.body.scrollHeight); }
|
||||
document.querySelectorAll('div, section, main, article').forEach(function (el) {
|
||||
try {
|
||||
if (el.scrollHeight > el.clientHeight + 40 && el.clientHeight > 120) {
|
||||
el.scrollTo({ top: el.scrollHeight, behavior: behavior });
|
||||
}
|
||||
} catch (e) {}
|
||||
});
|
||||
""")
|
||||
ok = True
|
||||
except Exception:
|
||||
try:
|
||||
page.scroll.to_bottom()
|
||||
ok = True
|
||||
except Exception:
|
||||
pass
|
||||
if settle:
|
||||
time.sleep(settle)
|
||||
return ok
|
||||
|
||||
|
||||
def wait_page_ready(page, keyword, timeout=20):
|
||||
"""等页面出现关键文本(等 SPA 渲染完成)。"""
|
||||
deadline = time.time() + timeout
|
||||
while time.time() < deadline:
|
||||
try:
|
||||
body = page.run_js('return document.body ? document.body.innerText : ""') or ''
|
||||
if keyword in body:
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
time.sleep(1)
|
||||
return False
|
||||
Reference in New Issue
Block a user