fix: 等待多地址列表稳定加载 (#189)
This commit is contained in:
@@ -233,8 +233,10 @@ def _pdd_text_targets(root: ET.Element, exact_text: str) -> list[Bounds]:
|
||||
return sorted(targets)
|
||||
|
||||
|
||||
def _address_modify_targets(root: ET.Element) -> list[Bounds]:
|
||||
"""多地址时只返回唯一“已设默认”卡片内的修改按钮。"""
|
||||
def _address_modify_target_info(
|
||||
root: ET.Element,
|
||||
) -> tuple[list[Bounds], dict[str, int]]:
|
||||
"""返回默认地址修改目标及不含地址原文的结构诊断。"""
|
||||
|
||||
address_cards = [
|
||||
card
|
||||
@@ -243,15 +245,37 @@ def _address_modify_targets(root: ET.Element) -> list[Bounds]:
|
||||
for card in list(recycler)
|
||||
if _pdd_text_targets(card, "修改")
|
||||
]
|
||||
all_targets = _pdd_text_targets(root, "修改")
|
||||
if len(address_cards) <= 1:
|
||||
return _pdd_text_targets(root, "修改")
|
||||
targets = all_targets
|
||||
default_card_count = sum(
|
||||
1 for card in address_cards if "已设默认" in _labels(card)
|
||||
)
|
||||
return targets, {
|
||||
"address_card_count": len(address_cards),
|
||||
"default_card_count": default_card_count,
|
||||
"all_modify_target_count": len(all_targets),
|
||||
}
|
||||
|
||||
default_cards = [
|
||||
card for card in address_cards if "已设默认" in _labels(card)
|
||||
]
|
||||
if len(default_cards) != 1:
|
||||
return []
|
||||
return _pdd_text_targets(default_cards[0], "修改")
|
||||
targets = []
|
||||
else:
|
||||
targets = _pdd_text_targets(default_cards[0], "修改")
|
||||
return targets, {
|
||||
"address_card_count": len(address_cards),
|
||||
"default_card_count": len(default_cards),
|
||||
"all_modify_target_count": len(all_targets),
|
||||
}
|
||||
|
||||
|
||||
def _address_modify_targets(root: ET.Element) -> list[Bounds]:
|
||||
"""多地址时只返回唯一“已设默认”卡片内的修改按钮。"""
|
||||
|
||||
targets, _diagnostics = _address_modify_target_info(root)
|
||||
return targets
|
||||
|
||||
|
||||
def _pdd_title_back_targets(root: ET.Element) -> list[Bounds]:
|
||||
@@ -1444,6 +1468,11 @@ class U2PddLivePurchaseAdapter(U2PddPurchaseAdapter, PddLivePurchaseAdapter):
|
||||
root = panel_root
|
||||
observed_page = _address_page_kind(root)
|
||||
candidate_count = 0
|
||||
target_diagnostics = {
|
||||
"address_card_count": 0,
|
||||
"default_card_count": 0,
|
||||
"all_modify_target_count": 0,
|
||||
}
|
||||
stable_target: Optional[Bounds] = None
|
||||
stable_reads = 0
|
||||
first_clicked_target: Optional[Bounds] = None
|
||||
@@ -1456,45 +1485,37 @@ class U2PddLivePurchaseAdapter(U2PddPurchaseAdapter, PddLivePurchaseAdapter):
|
||||
return root
|
||||
|
||||
if observed_page == "panel":
|
||||
targets = _address_modify_targets(root)
|
||||
targets, target_diagnostics = _address_modify_target_info(root)
|
||||
candidate_count = len(targets)
|
||||
if candidate_count != 1:
|
||||
raise PddPurchaseError(
|
||||
"PURCHASE_ADDRESS_EDIT_AMBIGUOUS",
|
||||
"收货地址页没有唯一可靠的修改按钮",
|
||||
step="purchase_update_shipping_address",
|
||||
diagnostics={
|
||||
"candidate_count": candidate_count,
|
||||
"click_attempts": click_attempts,
|
||||
"observed_page": observed_page,
|
||||
},
|
||||
)
|
||||
|
||||
current_target = targets[0]
|
||||
retry_target_changed = (
|
||||
first_clicked_target is not None
|
||||
and current_target != first_clicked_target
|
||||
)
|
||||
if retry_target_changed or click_attempts >= 2:
|
||||
stable_target = None
|
||||
stable_reads = 0
|
||||
elif current_target == stable_target:
|
||||
stable_reads += 1
|
||||
else:
|
||||
stable_target = current_target
|
||||
stable_reads = 1
|
||||
|
||||
if stable_reads >= 2:
|
||||
self._click_unique_address_target(
|
||||
targets,
|
||||
"PURCHASE_ADDRESS_EDIT_AMBIGUOUS",
|
||||
"收货地址页没有唯一可靠的修改按钮",
|
||||
current_target = targets[0]
|
||||
retry_target_changed = (
|
||||
first_clicked_target is not None
|
||||
and current_target != first_clicked_target
|
||||
)
|
||||
click_attempts += 1
|
||||
if first_clicked_target is None:
|
||||
first_clicked_target = current_target
|
||||
stable_target = None
|
||||
stable_reads = 0
|
||||
if retry_target_changed or click_attempts >= 2:
|
||||
stable_target = None
|
||||
stable_reads = 0
|
||||
elif current_target == stable_target:
|
||||
stable_reads += 1
|
||||
else:
|
||||
stable_target = current_target
|
||||
stable_reads = 1
|
||||
|
||||
if stable_reads >= 2:
|
||||
self._click_unique_address_target(
|
||||
targets,
|
||||
"PURCHASE_ADDRESS_EDIT_AMBIGUOUS",
|
||||
"收货地址页没有唯一可靠的修改按钮",
|
||||
)
|
||||
click_attempts += 1
|
||||
if first_clicked_target is None:
|
||||
first_clicked_target = current_target
|
||||
stable_target = None
|
||||
stable_reads = 0
|
||||
else:
|
||||
# 页面状态不明确时不能继续点击,避免误触其他页面。
|
||||
candidate_count = 0
|
||||
@@ -1509,13 +1530,24 @@ class U2PddLivePurchaseAdapter(U2PddPurchaseAdapter, PddLivePurchaseAdapter):
|
||||
"observed_page": observed_page,
|
||||
"click_attempts": click_attempts,
|
||||
"candidate_count": candidate_count,
|
||||
"stable_reads": stable_reads,
|
||||
**target_diagnostics,
|
||||
}
|
||||
artifact = self._save_last_xml("purchase-address-edit-timeout")
|
||||
if artifact is not None:
|
||||
diagnostics["artifacts"] = [artifact]
|
||||
ambiguous = observed_page == "panel" and candidate_count != 1
|
||||
raise PddPurchaseError(
|
||||
"PURCHASE_ADDRESS_PAGE_TIMEOUT",
|
||||
"收货地址页面切换或保存确认超时,未进入不可逆下单阶段",
|
||||
(
|
||||
"PURCHASE_ADDRESS_EDIT_AMBIGUOUS"
|
||||
if ambiguous
|
||||
else "PURCHASE_ADDRESS_PAGE_TIMEOUT"
|
||||
),
|
||||
(
|
||||
"收货地址页没有唯一可靠的修改按钮"
|
||||
if ambiguous
|
||||
else "收货地址页面切换或保存确认超时,未进入不可逆下单阶段"
|
||||
),
|
||||
step="purchase_update_shipping_address",
|
||||
diagnostics=diagnostics,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user