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.
This commit is contained in:
Misaka_Company
2026-01-23 13:52:52 +08:00
parent cf88800444
commit ebd53b168e

View File

@@ -142,28 +142,46 @@ class DiscreteMaterialPlanCleaner:
save_button_locator.wait_for(state="visible", timeout=10000) save_button_locator.wait_for(state="visible", timeout=10000)
inner_frame.get_by_text("展开").first.click() 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() #page2.pause()
for id in range(detail_count): 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() # 获取材料编码使用class精确定位取第一个非隐藏的input
# 提取"展开"页面中的内容 try:
detail_element = inner_frame.get_by_text(re.compile(r"^材料编码\d+$")) material_code_input = child_form.locator(".cbmaterialvid").locator("input").first
#移除detail_text中的换行符 material_code = material_code_input.input_value(timeout=5000)
detail_text = detail_element.inner_text().replace("\n", "") self._print(f"材料编码:{material_code}")
# 使用正则表达式提取括号中的数字 except Exception as e:
match = re.search(r"材料编码(\d+)", detail_text) self._print(f"材料编码:获取失败 - {e}")
if match:
match_result = int(match.group(1))
self._print(f"材料编码:{match_result}")
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()}") 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: 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: 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() #page2.pause()
elif detail_count == 0: elif detail_count == 0: