From ebd53b168ea4c846135e5b2648a5d50a1e0e58da Mon Sep 17 00:00:00 2001 From: Misaka_Company Date: Fri, 23 Jan 2026 13:52:52 +0800 Subject: [PATCH] refactor: improve locator precision and data extraction Refine element selection by introducing a specific parent container `.card-table-side-box` to enhance locator accuracy. Replace the previous text-parsing approach for material codes with direct input value retrieval. Additionally, add support for extracting cumulative pending and outbound quantities to improve data completeness. --- utils/离散备料计划维护数据清理.py | 46 +++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/utils/离散备料计划维护数据清理.py b/utils/离散备料计划维护数据清理.py index 460940a..2898074 100644 --- a/utils/离散备料计划维护数据清理.py +++ b/utils/离散备料计划维护数据清理.py @@ -142,28 +142,46 @@ class DiscreteMaterialPlanCleaner: save_button_locator.wait_for(state="visible", timeout=10000) inner_frame.get_by_text("展开").first.click() + + + # 获取展开后的父容器,基于它定位子元素更加精确 + # 父元素 class="card-table-side-box undefined" + child_form = inner_frame.locator(".card-table-side-box") + # 等待父容器变为可见 + child_form.wait_for(state="visible", timeout=5000) + self._print(f"父容器 .card-table-side-box 已找到") + + #page2.pause() for id in range(detail_count): - id_lable_locator= inner_frame.get_by_text("序号 " + str(id + 1)).wait_for(state="visible", timeout=10000) + id_lable_locator = child_form.get_by_text("序号 " + str(id + 1)) + id_lable_locator.wait_for(state="visible", timeout=10000) + self._print(f"处理 {id_lable_locator.inner_text()} ") - #page2.pause() - # 提取"展开"页面中的内容 - detail_element = inner_frame.get_by_text(re.compile(r"^材料编码\d+$")) - #移除detail_text中的换行符 - detail_text = detail_element.inner_text().replace("\n", "") - # 使用正则表达式提取括号中的数字 - match = re.search(r"材料编码(\d+)", detail_text) - if match: - match_result = int(match.group(1)) - self._print(f"材料编码:{match_result}") + # 获取材料编码(使用class精确定位,取第一个非隐藏的input) + try: + material_code_input = child_form.locator(".cbmaterialvid").locator("input").first + material_code = material_code_input.input_value(timeout=5000) + self._print(f"材料编码:{material_code}") + except Exception as e: + self._print(f"材料编码:获取失败 - {e}") - input_box = inner_frame.locator("div").filter(has_text=re.compile(r"^材料名称$")).nth(2).locator("input[type='text']") + # 获取材料名称 + input_box = child_form.locator("div").filter(has_text=re.compile(r"^材料名称$")).locator("input[type='text']") self._print(f"材料名称:{input_box.input_value()}") + # 获取累计待发数量 + input_box = child_form.locator("div").filter(has_text=re.compile(r"^累计待发数量$")).locator("input[type='text']") + self._print(f"累计待发数量:{input_box.input_value()}") + + # 获取累计出库数量 + input_box = child_form.locator("div").filter(has_text=re.compile(r"^累计出库数量$")).locator("input[type='text']") + self._print(f"累计出库数量:{input_box.input_value()}") + if id != detail_count - 1: - inner_frame.get_by_role("button").filter(has_text=re.compile(r"^$")).nth(2).click() + child_form.get_by_role("button").filter(has_text=re.compile(r"^$")).nth(2).click() else: - inner_frame.get_by_role("button").filter(has_text=re.compile(r"^$")).nth(4).click() + child_form.get_by_role("button").filter(has_text=re.compile(r"^$")).nth(4).click() #page2.pause() elif detail_count == 0: