feat: add wait mechanisms for delete and save operations in discrete material cleaner

This change adds robust wait mechanisms to prevent race conditions when
performing delete and save operations in the ERP system.

Changes:
- Add row number change detection after delete to confirm completion
- Add save button disappearance detection to confirm save completion
- Add row number range check (7000-8000) to skip specific rows
- Enable previously commented save button click
- Improve logging with detailed status messages
- Add timeout handling (10s for delete, 60s for save)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Misaka_Company
2026-02-25 11:45:31 +08:00
parent de25841364
commit 0a2c3c55c6

View File

@@ -153,9 +153,11 @@ class DiscreteMaterialPlanCleaner:
collapse_btn = button_wrapper.locator(".icon-celashouqi") collapse_btn = button_wrapper.locator(".icon-celashouqi")
last_row_number = None last_row_number = None
# page2.pause() # 调试用,正式运行时可删除
while True: while True:
# 稳定性检查:等待行号更新 # 稳定性检查:等待行号更新
current_row = self._get_input_value(child_form, r"^行号$") current_row = self._get_input_value(child_form, r"^行号$")
row_num_int = int(current_row)
if current_row == last_row_number: if current_row == last_row_number:
time.sleep(0.5) time.sleep(0.5)
@@ -163,17 +165,44 @@ class DiscreteMaterialPlanCleaner:
material_name = self._get_input_value(child_form, r"^材料名称") material_name = self._get_input_value(child_form, r"^材料名称")
pending_qty = self._get_input_value(child_form, r"^累计待发数量$") pending_qty = self._get_input_value(child_form, r"^累计待发数量$")
if material_code in self.to_delete_set: if material_code in self.to_delete_set:
self._log(f"发现匹配物料: {material_name} ({material_code})") self._log(f"发现匹配物料: {material_name} ({material_code})")
if not pending_qty or float(pending_qty) != 0: if (not pending_qty) and not (row_num_int>=7000 and row_num_int<8000) :
# 记录删除前的行号
old_row_number = current_row
delete_row_btn.click() delete_row_btn.click()
self._log(f"✅ 已点击删行") self._log(f"✅ 已点击删行,等待删除完成...")
# 等待行号变化(表示删除完成且新数据已加载)
max_wait_time = 10 # 最大等待10秒
start_time = time.time()
while time.time() - start_time < max_wait_time:
try:
new_row_number = self._get_input_value(child_form, r"^行号$")
if new_row_number != old_row_number:
self._log(f"✓ 删除完成,行号已从 {old_row_number} 变更为 {new_row_number}")
break
time.sleep(0.2) # 每200ms检查一次
except Exception as e:
self._log(f"获取新行号时出错: {e}", "warn")
time.sleep(0.2)
else:
self._log(f"⚠️ 等待删除完成超时({max_wait_time}秒)", "warn")
continue continue
else: elif row_num_int>=7000 and row_num_int<8000:
self._log(f"⚠️ 行号 {row_num_int} 在 7000-8000 范围内,跳过删除", "warn")
elif pending_qty:
self._log(f"⚠️ 待发数量为 {pending_qty},跳过删除", "warn") self._log(f"⚠️ 待发数量为 {pending_qty},跳过删除", "warn")
else:
self._log(f"⚠️ 不满足删除条件,跳过物料 {material_name} ({material_code})", "warn")
else: else:
self._log( self._log(
f"物料 {material_name} ({material_code}) 不在删除列表中,不做处理" f" 物料 {material_name} ({material_code}) 不在删除列表中,不做处理"
) )
if self._is_button_enabled(next_btn): if self._is_button_enabled(next_btn):
last_row_number = current_row last_row_number = current_row
@@ -183,7 +212,10 @@ class DiscreteMaterialPlanCleaner:
collapse_btn.click() collapse_btn.click()
# 执行最终保存逻辑(如业务需要) # 执行最终保存逻辑(如业务需要)
# save_button_locator.click() save_button_locator.click()
self._log("已点击保存,等待保存完成...")
save_button_locator.wait_for(state="hidden", timeout=60000) # 等待按钮消失超时60秒
self._log("✅ 保存成功(保存按钮已消失)")
else: else:
self._log("订单无备料计划数据,无需处理") self._log("订单无备料计划数据,无需处理")
elif detail_status == "完成": elif detail_status == "完成":