fix(yunda): locate visible business iframe by content probe, fixes multi-iframe strict-mode violation

This commit is contained in:
Misaka_Company
2026-08-04 17:04:03 +08:00
parent 2387593eda
commit e99f311bca

View File

@@ -172,6 +172,37 @@ def _resolve_export_frame(ws_frame):
return ws_frame.frame_locator('iframe[name="myFrame"]') return ws_frame.frame_locator('iframe[name="myFrame"]')
def _resolve_business_frame(page, probe_selector, label):
"""定位当前「可见」的业务 iframe返回 Frame用法与 FrameLocator 兼容)。
韵达门户每次切换菜单都会保留历史隐藏 iframedisplay:none因此不能按
`section iframe` 全量匹配会命中多个strict mode 报错)。这里遍历
page.frames取「可见」且「包含指定控件」的 frameprobe_selector 需是
目标页面独有的控件:应到=#startTime实到=#startDate导出服务=.datagrid-view2。
"""
print(f">> 正在定位业务 iframe{label}...")
deadline = time.monotonic() + 20
last_err = None
while time.monotonic() < deadline:
for frame in page.frames:
if frame is page.main_frame:
continue
try:
if not frame.frame_element().is_visible():
continue
except Exception as e:
last_err = e
continue
try:
if frame.locator(probe_selector).count() > 0:
print(f" ✅ 已定位业务 iframe{label}: {frame.url[:100]}")
return frame
except Exception as e:
last_err = e
page.wait_for_timeout(500)
raise RuntimeError(f"未定位到可见业务 iframe{label}: {last_err}")
def yunda_login(page): def yunda_login(page):
"""韵达自动登录:未登录则填充表单并提交,已登录则跳过。""" """韵达自动登录:未登录则填充表单并提交,已登录则跳过。"""
print(">> 正在检查韵达登录状态...") print(">> 正在检查韵达登录状态...")
@@ -270,7 +301,7 @@ def yunda_expected_download_impl(page, force=False, date=None):
yunda_smart_menu_click(page, ["运营管理", "进站管理", "进站交接单查询"]) yunda_smart_menu_click(page, ["运营管理", "进站管理", "进站交接单查询"])
print(">> 正在定位【进站交接单查询】iframe...") print(">> 正在定位【进站交接单查询】iframe...")
ws_frame = page.frame_locator("section iframe") ws_frame = _resolve_business_frame(page, "#startTime", "进站交接单查询")
ws_frame.locator("#startTime").wait_for(state="attached", timeout=15000) ws_frame.locator("#startTime").wait_for(state="attached", timeout=15000)
print("✅ 进站交接单查询页面就绪") print("✅ 进站交接单查询页面就绪")
@@ -527,7 +558,7 @@ def yunda_actual_download_impl(page, date=None):
yunda_smart_menu_click(page, ["报表管理", "扫描记录查询"]) yunda_smart_menu_click(page, ["报表管理", "扫描记录查询"])
print(">> 正在定位【扫描记录查询】iframe...") print(">> 正在定位【扫描记录查询】iframe...")
ws_frame = page.frame_locator("section iframe") ws_frame = _resolve_business_frame(page, "#startDate", "扫描记录查询")
ws_frame.locator( ws_frame.locator(
".no-records-found", has_text="没有找到匹配的记录" ".no-records-found", has_text="没有找到匹配的记录"
@@ -714,7 +745,7 @@ def _yunda_poll_and_download_tasks(page, export_times, download_dir, final_filen
print("\n>> 正在前往【导出服务】界面...") print("\n>> 正在前往【导出服务】界面...")
yunda_smart_menu_click(page, ["基础数据", "导出服务"]) yunda_smart_menu_click(page, ["基础数据", "导出服务"])
export_ws_frame = page.frame_locator("section iframe") export_ws_frame = _resolve_business_frame(page, ".datagrid-view2", "导出服务")
export_ws_frame.get_by_role("cell", name="模块名称", exact=True).wait_for( export_ws_frame.get_by_role("cell", name="模块名称", exact=True).wait_for(
state="visible", timeout=15000 state="visible", timeout=15000