diff --git a/playwright_automation.py b/playwright_automation.py index cefcbaf..7c2d9d5 100755 --- a/playwright_automation.py +++ b/playwright_automation.py @@ -424,7 +424,7 @@ class PlaywrightAutomation: # 等待跳转 # self.log("等待登录处理...") # 精简日志 - self.page.wait_for_load_state('networkidle', timeout=30000) # 增加到30秒 + self.page.wait_for_load_state('networkidle', timeout=10000) # 优化为10秒 # 检查登录结果 current_url = self.page.url @@ -823,7 +823,7 @@ class PlaywrightAutomation: self.log(f"导航到 '{browse_type}' 页面...") try: # 等待页面完全加载 - time.sleep(2) + time.sleep(0.5) self.log(f"当前URL: {self.main_page.url}") except Exception as e: self.log(f"获取URL失败: {str(e)}") @@ -835,7 +835,7 @@ class PlaywrightAutomation: # 如果只是导航(用于截图),切换完成后直接返回 if navigate_only: - time.sleep(1) # 等待页面稳定 + time.sleep(0.3) # 等待页面稳定 result.success = True return result @@ -867,27 +867,21 @@ class PlaywrightAutomation: except Exception: # Bug fix: 明确捕获Exception self.log("等待表格超时,继续尝试...") - # 额外等待,确保AJAX内容加载完成 - # 第一页等待更长时间,因为是首次加载(并发时尤其���要) - if current_page == 1 and total_items == 0: - time.sleep(3.0) - else: - time.sleep(1.0) + # 等待页面网络空闲,确保AJAX加载完成 + try: + self.page.wait_for_load_state('networkidle', timeout=5000) + except Exception: + pass # 超时继续,不阻塞 - # 获取内容行数量(带重试机制,避免AJAX加载慢导致误判) - # 第一页使用更多重试次数(8次×3秒=24秒),处理高并发时的慢加载 - # 后续页使用3次×1.5秒=4.5秒 - max_retries = 8 if (current_page == 1 and total_items == 0) else 3 - retry_wait = 3.0 if (current_page == 1 and total_items == 0) else 1.5 + # 获取内容行数量(简化重试:2次快速检测) rows_count = 0 - for retry in range(max_retries): + for retry in range(2): rows_locator = self.page.locator("//table[@class='ltable']/tbody/tr[position()>1 and count(td)>=5]") rows_count = rows_locator.count() if rows_count > 0: break - if retry < max_retries - 1: - self.log(f"未检测到内容,等待后重试... ({retry+1}/{max_retries})") - time.sleep(retry_wait) + if retry == 0: + time.sleep(0.5) # 仅重试一次,等待0.5秒 if rows_count == 0: self.log("当前页面没有内容") diff --git a/services/screenshots.py b/services/screenshots.py index 2a58698..a66cefd 100644 --- a/services/screenshots.py +++ b/services/screenshots.py @@ -130,11 +130,11 @@ def take_screenshot_for_account( if "center.aspx" not in current_url: raise RuntimeError(f"unexpected_iframe_url:{current_url}") try: - iframe.wait_for_load_state("networkidle", timeout=30000) + iframe.wait_for_load_state("networkidle", timeout=10000) except Exception: pass try: - iframe.wait_for_selector("table.ltable", timeout=20000) + iframe.wait_for_selector("table.ltable", timeout=5000) except Exception: pass else: @@ -144,11 +144,11 @@ def take_screenshot_for_account( if "center.aspx" not in current_url: raise RuntimeError(f"unexpected_url:{current_url}") try: - automation.main_page.wait_for_load_state("networkidle", timeout=30000) + automation.main_page.wait_for_load_state("networkidle", timeout=10000) except Exception: pass try: - automation.main_page.wait_for_selector("table.ltable", timeout=20000) + automation.main_page.wait_for_selector("table.ltable", timeout=5000) except Exception: pass navigated = True diff --git a/services/tasks.py b/services/tasks.py index fa0a75a..717e54a 100644 --- a/services/tasks.py +++ b/services/tasks.py @@ -857,7 +857,6 @@ def run_task(user_id, account_id, browse_type, enable_screenshot=True, source="m }, }, ) - time.sleep(2) browse_result_dict = {"total_items": result.total_items, "total_attachments": result.total_attachments} screenshot_submitted = True threading.Thread(