"""等待策略 Mixin""" from typing import Callable from playwright.sync_api import expect class WaitMixin: """等待策略相关操作 提供多种等待元素出现、隐藏、可用等状态的方法。 """ def wait(self, milliseconds: int) -> None: """固定等待时间 Args: milliseconds: 等待毫秒数 """ self.page.wait_for_timeout(milliseconds) def wait_for_element( self, selector: str, state: str = "visible", timeout: int = None ): """等待元素出现并返回 locator Args: selector: 元素选择器 state: 等待状态 (visible/hidden/attached/detached) timeout: 超时时间(毫秒) Returns: Locator: Playwright Locator 对象 """ locator = self.page.locator(selector) locator.wait_for(state=state, timeout=timeout or self.timeout) return locator def wait_for_element_hidden(self, selector: str, timeout: int = None) -> None: """等待元素隐藏 Args: selector: 元素选择器 timeout: 超时时间(毫秒) """ self.page.locator(selector).wait_for( state="hidden", timeout=timeout or self.timeout ) def wait_for_element_detached(self, selector: str, timeout: int = None) -> None: """等待元素从DOM中移除 Args: selector: 元素选择器 timeout: 超时时间(毫秒) """ self.page.locator(selector).wait_for( state="detached", timeout=timeout or self.timeout ) def wait_for_element_enabled(self, selector: str, timeout: int = None) -> None: """等待元素可用 Args: selector: 元素选择器 timeout: 超时时间(毫秒) """ locator = self.page.locator(selector) expect(locator).to_be_enabled(timeout=timeout or self.timeout) def wait_for_element_disabled(self, selector: str, timeout: int = None) -> None: """等待元素禁用 Args: selector: 元素选择器 timeout: 超时时间(毫秒) """ locator = self.page.locator(selector) expect(locator).not_to_be_enabled(timeout=timeout or self.timeout) def wait_for_text(self, selector: str, text: str, timeout: int = None) -> None: """等待元素包含指定文本 Args: selector: 元素选择器 text: 期望文本 timeout: 超时时间(毫秒) """ locator = self.page.locator(selector) expect(locator).to_have_text(text, timeout=timeout or self.timeout) def wait_for_value(self, selector: str, value: str, timeout: int = None) -> None: """等待输入框包含指定值 Args: selector: 元素选择器 value: 期望值 timeout: 超时时间(毫秒) """ locator = self.page.locator(selector) expect(locator).to_have_value(value, timeout=timeout or self.timeout) def wait_for_selector_safe( self, selector: str, state: str = "visible", timeout: int = 5000 ) -> bool: """安全等待元素:返回布尔值而非抛异常 Args: selector: 元素选择器 state: 等待状态 (visible/hidden/attached/detached) timeout: 超时毫秒 Returns: bool: 元素是否达到预期状态 """ try: self.find(selector).wait_for(state=state, timeout=timeout) return True except Exception: return False def smart_wait(self, selector: str, state: str = "visible", timeout: int = 5000) -> bool: """智能等待元素(安全版本) Args: selector: 元素选择器 state: 等待状态 timeout: 超时毫秒 Returns: bool: 元素是否达到预期状态 """ return self.wait_for_selector_safe(selector, state, timeout) def wait_until( self, condition: Callable[[], bool], timeout: int = None, interval: int = 500 ) -> bool: """轮询等待条件满足 Args: condition: 返回布尔值的条件函数 timeout: 总超时毫秒 interval: 轮询间隔毫秒 Returns: bool: 条件是否在超时前满足 """ import time start = time.time() * 1000 timeout = timeout or self.timeout while time.time() * 1000 - start < timeout: try: if condition(): return True except Exception: pass self.wait(interval) return False def wait_for_response(self, url_pattern: str, timeout: int = None): """等待特定响应 Args: url_pattern: URL 匹配模式 timeout: 超时时间(毫秒) Returns: Response: Playwright Response 对象 """ with self.page.expect_response( url_pattern, timeout=timeout or self.timeout ) as response: return response.value def wait_for_request(self, url_pattern: str, timeout: int = None): """等待特定请求 Args: url_pattern: URL 匹配模式 timeout: 超时时间(毫秒) Returns: Request: Playwright Request 对象 """ with self.page.expect_request( url_pattern, timeout=timeout or self.timeout ) as request: return request.value