fix: 所有click加force=True,绕过遮罩层/倒计时覆盖层拦截
日志显示'subtree intercepts pointer events'导致点击失败 force=True直接在元素坐标触发点击,不受遮罩层影响 同时增加按钮等待和点击超时时间
This commit is contained in:
@@ -151,13 +151,14 @@ async def _phase1_purchase(page, tab_index=0):
|
|||||||
for attempt in range(PHASE1_RETRIES):
|
for attempt in range(PHASE1_RETRIES):
|
||||||
try:
|
try:
|
||||||
await page.reload(wait_until='commit', timeout=8000)
|
await page.reload(wait_until='commit', timeout=8000)
|
||||||
await asyncio.sleep(0.3)
|
# 等 DOM 关键元素出现,比纯 commit 多等一点
|
||||||
|
await asyncio.sleep(0.5 if attempt == 0 else 0.3)
|
||||||
|
|
||||||
buy_btn = None
|
buy_btn = None
|
||||||
for text in BUY_TEXTS:
|
for text in BUY_TEXTS:
|
||||||
loc = page.get_by_text(text, exact=False)
|
loc = page.get_by_text(text, exact=False)
|
||||||
try:
|
try:
|
||||||
await loc.first.wait_for(state="visible", timeout=1500)
|
await loc.first.wait_for(state="visible", timeout=2000)
|
||||||
buy_btn = loc.first
|
buy_btn = loc.first
|
||||||
break
|
break
|
||||||
except Exception:
|
except Exception:
|
||||||
@@ -169,15 +170,16 @@ async def _phase1_purchase(page, tab_index=0):
|
|||||||
continue
|
continue
|
||||||
return f"P1-tab{tab_index}: 未找到购买按钮"
|
return f"P1-tab{tab_index}: 未找到购买按钮"
|
||||||
|
|
||||||
await buy_btn.click(timeout=2000)
|
# force=True 绕过遮罩层/倒计时覆盖层的拦截
|
||||||
|
await buy_btn.click(timeout=3000, force=True)
|
||||||
|
|
||||||
# SKU 弹窗
|
# SKU 弹窗
|
||||||
await _handle_sku(page)
|
await _handle_sku(page)
|
||||||
|
|
||||||
# 提交订单
|
# 提交订单
|
||||||
submit_btn = page.get_by_text("提交订单")
|
submit_btn = page.get_by_text("提交订单")
|
||||||
await submit_btn.wait_for(state="visible", timeout=6000)
|
await submit_btn.wait_for(state="visible", timeout=8000)
|
||||||
await submit_btn.click()
|
await submit_btn.click(force=True)
|
||||||
return f"P1-tab{tab_index}: 抢购请求已提交"
|
return f"P1-tab{tab_index}: 抢购请求已提交"
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -223,7 +225,7 @@ async def _phase2_cart_purchase(page, cart_item_id):
|
|||||||
# 点击商品前面的勾选框
|
# 点击商品前面的勾选框
|
||||||
checkbox = item_warp.locator('.checkbox').first
|
checkbox = item_warp.locator('.checkbox').first
|
||||||
if await checkbox.count() > 0:
|
if await checkbox.count() > 0:
|
||||||
await checkbox.click()
|
await checkbox.click(force=True)
|
||||||
selected = True
|
selected = True
|
||||||
await asyncio.sleep(0.3)
|
await asyncio.sleep(0.3)
|
||||||
|
|
||||||
@@ -232,7 +234,7 @@ async def _phase2_cart_purchase(page, cart_item_id):
|
|||||||
try:
|
try:
|
||||||
select_all = page.get_by_text("全选", exact=False)
|
select_all = page.get_by_text("全选", exact=False)
|
||||||
if await select_all.count() > 0:
|
if await select_all.count() > 0:
|
||||||
await select_all.first.click()
|
await select_all.first.click(force=True)
|
||||||
selected = True
|
selected = True
|
||||||
await asyncio.sleep(0.3)
|
await asyncio.sleep(0.3)
|
||||||
except Exception:
|
except Exception:
|
||||||
@@ -244,7 +246,7 @@ async def _phase2_cart_purchase(page, cart_item_id):
|
|||||||
first_cb = page.locator(
|
first_cb = page.locator(
|
||||||
'.item_warp .checkbox').first
|
'.item_warp .checkbox').first
|
||||||
if await first_cb.count() > 0:
|
if await first_cb.count() > 0:
|
||||||
await first_cb.click()
|
await first_cb.click(force=True)
|
||||||
selected = True
|
selected = True
|
||||||
await asyncio.sleep(0.3)
|
await asyncio.sleep(0.3)
|
||||||
except Exception:
|
except Exception:
|
||||||
@@ -267,7 +269,7 @@ async def _phase2_cart_purchase(page, cart_item_id):
|
|||||||
continue
|
continue
|
||||||
return "P2: 未找到结算按钮"
|
return "P2: 未找到结算按钮"
|
||||||
|
|
||||||
await settle_btn.click(timeout=2000)
|
await settle_btn.click(timeout=3000, force=True)
|
||||||
|
|
||||||
# 等待跳转到订单确认页
|
# 等待跳转到订单确认页
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
@@ -275,7 +277,7 @@ async def _phase2_cart_purchase(page, cart_item_id):
|
|||||||
# 提交订单
|
# 提交订单
|
||||||
submit_btn = page.get_by_text("提交订单")
|
submit_btn = page.get_by_text("提交订单")
|
||||||
await submit_btn.wait_for(state="visible", timeout=8000)
|
await submit_btn.wait_for(state="visible", timeout=8000)
|
||||||
await submit_btn.click()
|
await submit_btn.click(force=True)
|
||||||
return "P2-购物车: 抢购请求已提交"
|
return "P2-购物车: 抢购请求已提交"
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -294,16 +296,16 @@ async def _handle_sku(page):
|
|||||||
"""处理 SKU 选择弹窗"""
|
"""处理 SKU 选择弹窗"""
|
||||||
try:
|
try:
|
||||||
confirm_btn = page.get_by_text("确定", exact=True)
|
confirm_btn = page.get_by_text("确定", exact=True)
|
||||||
await confirm_btn.first.wait_for(state="visible", timeout=1500)
|
await confirm_btn.first.wait_for(state="visible", timeout=2000)
|
||||||
sku_sel = ('.sku-item:not(.disabled), '
|
sku_sel = ('.sku-item:not(.disabled), '
|
||||||
'.sku_item:not(.disabled), '
|
'.sku_item:not(.disabled), '
|
||||||
'[class*="sku"] [class*="item"]'
|
'[class*="sku"] [class*="item"]'
|
||||||
':not([class*="disabled"])')
|
':not([class*="disabled"])')
|
||||||
sku_items = page.locator(sku_sel)
|
sku_items = page.locator(sku_sel)
|
||||||
if await sku_items.count() > 0:
|
if await sku_items.count() > 0:
|
||||||
await sku_items.first.click()
|
await sku_items.first.click(force=True)
|
||||||
await asyncio.sleep(0.1)
|
await asyncio.sleep(0.1)
|
||||||
await confirm_btn.first.click(timeout=2000)
|
await confirm_btn.first.click(timeout=2000, force=True)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user