This commit is contained in:
ddrwode
2026-02-27 15:37:22 +08:00
parent 72aa8855e8
commit f418f52117

120
test1.py
View File

@@ -775,48 +775,93 @@ def submit_phone(
if not bg_img or not piece_img or not slider:
raise RuntimeError("验证码关键元素缺失(背景图/拼图块/滑块)")
bg_src = wait_for_data_src(bg_img, timeout=10)
piece_src = wait_for_data_src(piece_img, timeout=10)
bg_bytes = parse_data_url(bg_src)
piece_bytes = parse_data_url(piece_src)
if len(bg_bytes) < 100 or len(piece_bytes) < 100:
raise RuntimeError("验证码图片数据异常")
match = calc_drag_distance_from_bytes(
bg_bytes, piece_bytes, alpha_threshold=alpha_threshold
)
layout = _get_slider_layout_metrics(page, bg_img, slider)
bg_display_w = layout["bg_display_w"] if layout["bg_display_w"] > 0 else match["bg_width"]
scale = float(bg_display_w) / max(1, match["bg_width"])
track_max = int(layout["track_max"])
move_distances = _build_move_distance_candidates(
match=match,
scale=scale,
distance_adjust=int(distance_adjust),
track_max=track_max,
)
# 6. 多候选位移拖动并等待 getYanZhenMa 响应
# 6. 每次重试前重新抓取验证码图片并重算位移(不复用旧距离)
deadline = time.time() + 15.0
last_reason = ""
attempts: list[dict[str, Any]] = []
max_attempts = 6
last_bg_bytes = b""
last_piece_bytes = b""
last_match: dict[str, Any] = {}
last_layout: dict[str, Any] = {}
last_move_distances: list[int] = []
for idx in range(1, max_attempts + 1):
if time.time() >= deadline:
break
bg_img = find_first(page, ["css:.verify-img-panel img"], timeout=2)
piece_img = find_first(page, ["css:.verify-sub-block img"], timeout=2)
slider = find_first(page, ["css:.verify-move-block"], timeout=2)
for idx, move_distance in enumerate(move_distances, start=1):
attempt: dict[str, Any] = {
"index": idx,
"move_distance": int(move_distance),
"boundary_hit": bool(track_max >= 0 and move_distance in (0, track_max)),
"move_distance": None,
"boundary_hit": False,
"packet_received": False,
"toast": "",
"interface_error": "",
"verify_bar_visible_after": False,
}
slider = find_first(page, ["css:.verify-move-block"], timeout=2)
if not slider:
attempt["interface_error"] = "未找到滑块元素"
if not bg_img or not piece_img or not slider:
last_reason = "重试时未找到验证码关键元素(背景图/拼图块/滑块)"
attempt["interface_error"] = last_reason
attempts.append(attempt)
break
try:
bg_src = wait_for_data_src(bg_img, timeout=4)
piece_src = wait_for_data_src(piece_img, timeout=4)
bg_bytes = parse_data_url(bg_src)
piece_bytes = parse_data_url(piece_src)
except Exception as e:
last_reason = f"重试时读取验证码图片失败: {e}"
attempt["interface_error"] = last_reason
attempts.append(attempt)
continue
if len(bg_bytes) < 100 or len(piece_bytes) < 100:
last_reason = f"验证码图片数据异常: bg={len(bg_bytes)}B piece={len(piece_bytes)}B"
attempt["interface_error"] = last_reason
attempts.append(attempt)
continue
try:
match = calc_drag_distance_from_bytes(
bg_bytes, piece_bytes, alpha_threshold=alpha_threshold
)
except Exception as e:
last_reason = f"重试时位移计算失败: {e}"
attempt["interface_error"] = last_reason
attempts.append(attempt)
continue
layout = _get_slider_layout_metrics(page, bg_img, slider)
bg_display_w = layout["bg_display_w"] if layout["bg_display_w"] > 0 else match["bg_width"]
scale = float(bg_display_w) / max(1, match["bg_width"])
track_max = int(layout["track_max"])
move_distances = _build_move_distance_candidates(
match=match,
scale=scale,
distance_adjust=int(distance_adjust),
track_max=track_max,
)
pick_index = min(max(0, idx - 1), len(move_distances) - 1)
move_distance = int(move_distances[pick_index])
attempt["move_distance"] = move_distance
attempt["boundary_hit"] = bool(track_max >= 0 and move_distance in (0, track_max))
attempt["confidence_ratio"] = float(match.get("confidence_ratio", 0) or 0)
attempt["candidate_count"] = len(move_distances)
# 记录最后一次有效计算,用于失败样本保存与分析
last_bg_bytes = bg_bytes
last_piece_bytes = piece_bytes
last_match = match
last_layout = layout
last_move_distances = move_distances
drag_slider(page, slider, move_distance)
time.sleep(0.2)
@@ -859,12 +904,9 @@ def submit_phone(
attempt["interface_error"] = last_reason if not attempt["interface_error"] else attempt["interface_error"]
attempts.append(attempt)
if time.time() >= deadline:
break
analysis = _analyze_slider_failure(
match=match,
layout=layout,
match=last_match,
layout=last_layout,
attempts=attempts,
last_reason=last_reason,
)
@@ -872,11 +914,11 @@ def submit_phone(
try:
case_dir = _save_failure_artifacts(
page=page,
bg_bytes=bg_bytes,
piece_bytes=piece_bytes,
match=match,
layout=layout,
move_distances=move_distances,
bg_bytes=last_bg_bytes,
piece_bytes=last_piece_bytes,
match=last_match,
layout=last_layout,
move_distances=last_move_distances,
attempts=attempts,
last_reason=last_reason,
analysis=analysis,