Playwright Python Locator API

📚 Playwright Python Locator API 速查手册#

按照官方文档原有分类整理,包含方法名用途说明调用格式单句示例


📂 集合操作#

方法说明调用格式示例
all()返回匹配元素数组locator.all()for li in page.get_by_role('listitem').all(): li.click()
all_inner_texts()所有元素的 innerText 数组locator.all_inner_texts()texts = links.all_inner_texts()
all_text_contents()所有元素的 textContent 数组locator.all_text_contents()texts = links.all_text_contents()

🔍 组合 / 过滤#

方法说明调用格式示例
and_(locator)同时匹配两个 Locatorlocator.and_(other_locator)btn = page.get_by_role("button").and_(page.get_by_title("OK"))
or_(locator)匹配任意一个 Locatorlocator.or_(other_locator)loc = a.or_(b)
filter(...)按条件过滤locator.filter(has_text="foo")rows.filter(has=btn)
nth(index)第 n 个元素(从 0 开始)locator.nth(2)third = items.nth(2)

🧾 元素信息#

方法说明调用格式示例
aria_snapshot()获取 ARIA 快照locator.aria_snapshot()snap = el.aria_snapshot()
bounding_box()获取元素位置尺寸locator.bounding_box()box = btn.bounding_box()
get_attribute(name)获取属性值locator.get_attribute("href")href = link.get_attribute("href")
inner_html()获取 innerHTMLlocator.inner_html()html = el.inner_html()
inner_text()获取 innerTextlocator.inner_text()text = el.inner_text()
text_content()获取 textContentlocator.text_content()txt = el.text_content()
input_value()获取输入值locator.input_value()val = input.input_value()
count()匹配元素数量locator.count()num = items.count()

✅ 状态判断#

方法说明调用格式示例
is_visible() / is_hidden()可见 / 隐藏状态locator.is_visible()if el.is_visible(): ...
is_checked()是否选中locator.is_checked()if cb.is_checked(): ...
is_disabled() / is_enabled()禁用 / 启用状态locator.is_disabled()if btn.is_enabled(): ...
is_editable()是否可编辑locator.is_editable()if input.is_editable(): ...

🎯 操作动作#

方法说明调用格式示例
click() / dblclick()单击 / 双击locator.click()btn.click()
tap()触摸点击locator.tap()btn.tap()
hover()鼠标悬停locator.hover()link.hover()
press(key)按键locator.press("Enter")input.press("Enter")
press_sequentially(text)模拟逐字键入locator.press_sequentially("abc")input.press_sequentially("hello")
fill(value)填充locator.fill("text")username.fill("john")
clear()清空locator.clear()search.clear()
check() / uncheck()勾选 / 取消locator.check()cb.uncheck()
set_checked(bool)设选中状态locator.set_checked(True)cb.set_checked(False)
set_input_files(...)上传文件locator.set_input_files("file.txt")upload.set_input_files(["a.txt","b.txt"])
select_option(...)选择下拉项locator.select_option("value")sel.select_option(label="blue")
select_text()选中文本locator.select_text()input.select_text()
drag_to(target)拖拽到目标locator.drag_to(target)src.drag_to(dst)
scroll_into_view_if_needed()滚动到可见位置locator.scroll_into_view_if_needed()el.scroll_into_view_if_needed()
screenshot(...)元素截图locator.screenshot(path="img.png")img = el.screenshot()
focus() / blur()聚焦 / 失焦locator.focus()input.blur()

⚡ 事件#

方法说明调用格式示例
dispatch_event(type, init)触发事件locator.dispatch_event("click")el.dispatch_event("mouseover")

🧭 定位扩展#

方法说明调用格式示例
locator(selector)在当前结果中查找locator.locator("span")row.locator(".cell")
frame_locator(selector)进入 iframepage.frame_locator("iframe")frm.locator("button").click()

🛠 评估执行#

方法说明调用格式示例
evaluate(js, arg)在浏览器执行 JSlocator.evaluate("el=>el.textContent")txt = el.evaluate("e=>e.id")
evaluate_all(js, arg)所有匹配元素执行 JSlocator.evaluate_all("(els)=>els.length")count = loc.evaluate_all(...)
evaluate_handle(js, arg)返回 JSHandlelocator.evaluate_handle("el=>el")handle = el.evaluate_handle(...)

⏳ 等待#

方法说明调用格式示例
wait_for(state="visible")等待状态变化locator.wait_for(state="hidden")loading.wait_for(state="detached")

🐞 调试辅助#

方法说明调用格式示例
highlight()高亮显示locator.highlight()btn.highlight()
describe(desc)添加定位器描述locator.describe("desc")btn.describe("提交按钮")

🎯 特定条件定位#

方法说明调用格式示例
get_by_alt_text(text)按图片 altpage.get_by_alt_text("logo")logo.click()
get_by_label(text)按 labelpage.get_by_label("Username")user.fill("john")
get_by_placeholder(text)按 placeholderpage.get_by_placeholder("email")email.fill("me@x.com")
get_by_role(role, name=...)按 ARIA rolepage.get_by_role("button", name="Submit")submit.click()
get_by_test_id(id)按 data-testidpage.get_by_test_id("id")btn.click()
get_by_text(text)按文本内容page.get_by_text("Hello")div.get_by_text("world")
get_by_title(text)按 title 属性page.get_by_title("Issues count")count_el.get_by_title("数量")