Files
playwrite/tools/locator_helper.py
Misaka_Company 60b6fecbb3 chore: move scripts to tools directory
Move analyze_excel.py, excel_to_markdown.py, and locator_helper.py
into the tools/ subdirectory to improve project organization.
2026-01-23 15:12:33 +08:00

172 lines
5.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
元素定位辅助工具 - 用于快速验证定位是否有效
"""
from playwright.sync_api import Page, Frame, Locator
def debug_locator(frame: Frame, locator: Locator, timeout: int = 5000):
"""
调试定位器 - 检查定位是否有效并显示详细信息
参数:
frame: 页面或iframe对象
locator: 定位器对象
timeout: 超时时间(毫秒)
返回:
bool: 定位是否成功
"""
print(f"\n验证定位: {locator}")
print("=" * 50)
try:
# 检查数量
count = locator.count()
print(f"元素数量: {count}")
if count == 0:
print("✗ 未找到任何匹配元素")
return False
# 检查第一个元素是否可见
first_visible = locator.first.is_visible(timeout=timeout)
print(f"第一个元素可见: {first_visible}")
# 获取文本内容
for i in range(min(3, count)): # 最多显示3个
element = locator.nth(i)
try:
if element.is_visible(timeout=1000):
text = element.inner_text(timeout=1000)
print(f" 元素{i + 1}文本: {text[:100] if len(text) > 100 else text}")
else:
print(f" 元素{i + 1}: 存在但不可见")
except:
print(f" 元素{i + 1}: 无法获取信息")
print("=" * 50)
return True
except Exception as e:
print(f"✗ 验证失败: {e}")
print("=" * 50)
return False
def try_multiple_locators(frame: Frame, selectors: list[str], timeout: int = 5000) -> Locator:
"""
尝试多个选择器,返回第一个有效的定位器
参数:
frame: 页面或iframe对象
selectors: 选择器列表
timeout: 超时时间(毫秒)
返回:
第一个有效的 Locator如果没有则返回 None
"""
print(f"\n尝试 {len(selectors)} 个定位方式...")
print("-" * 50)
for i, selector in enumerate(selectors):
print(f"[{i + 1}] 尝试: {selector}")
try:
locator = frame.locator(selector)
count = locator.count()
visible_count = sum(1 for j in range(count) if locator.nth(j).is_visible(timeout=1000))
print(f" 找到 {count} 个元素,其中 {visible_count} 个可见")
if visible_count > 0:
print(f" ✓ 使用此定位器")
print("-" * 50)
return locator
except Exception as e:
print(f" ✗ 失败: {e}")
print("-" * 50)
return None
def interactive_locate(frame: Frame):
"""
交互式定位调试 - 进入交互模式测试定位表达式
"""
print("\n进入交互式定位调试模式")
print("输入定位表达式,输入 'q''quit' 退出")
print("-" * 50)
while True:
try:
selector = input("\n>>> ")
selector = selector.strip()
if selector.lower() in ('q', 'quit'):
break
if not selector:
continue
debug_locator(frame, frame.locator(selector))
except KeyboardInterrupt:
break
except Exception as e:
print(f"错误: {e}")
print("退出调试模式")
# 示例使用代码(单独运行此文件时的演示)
if __name__ == "__main__":
from playwright.sync_api import sync_playwright
import re
with sync_playwright() as pw:
browser = pw.chromium.launch(headless=False)
context = browser.new_context(ignore_https_errors=True)
page = context.new_page()
# 登录
page.goto("https://68.11.34.30:8082/yonbip/resources/uap/rbac/login/main/index.html")
main_frame = page.locator("#forwardFrame").content_frame
main_frame.get_by_role("textbox", name="用户名").fill("BLDpengqiangqiang")
main_frame.get_by_role("textbox", name="密码").fill("Cqbld123456.")
main_frame.get_by_role("button", name="登录").click()
confirm_btn = main_frame.get_by_role("button", name="确定")
if confirm_btn.count() > 0:
confirm_btn.click()
# 循环切换 Inspector 和交互式调试
print("\n" + "=" * 60)
print("元素定位调试工具")
print("=" * 60)
print("循环模式:")
print(" 1. Inspector 窗口 - 使用浏览器录制/选择元素")
print(" 2. 交互式调试 - 验证定位表达式")
print(" 输入 'q''quit' 退出程序\n")
while True:
# 1. 打开 Inspector 窗口
print("\n【打开 Inspector 窗口】")
print("请在 Inspector 中获取元素定位器,然后关闭 Inspector 继续...")
page.pause()
# 2. 进入交互式定位调试
print("\n【进入交互式定位调试】")
interactive_locate(main_frame)
# 询问是否继续
choice = input("\n是否继续下一轮调试?(y/n/q): ").strip().lower()
if choice in ('n', 'q', 'quit'):
print("退出程序")
break
elif choice in ('y', ''): # 默认继续
continue
else:
print("未知选项,退出程序")
break
context.close()
browser.close()