Playwright Python 输入操作指南

Playwright Python 输入操作指南

1. 简介

Playwright 可与 HTML 输入元素交互,包括文本输入、复选框、单选框、下拉选择、鼠标点击、键盘输入、文件上传、聚焦元素、拖拽和滚动等。


2. 主要操作方法

2.1 文本输入

  • 使用 locator.fill() 填充 <input><textarea>[contenteditable]
page.get_by_role("textbox").fill("Peter")
page.get_by_label("Birth date").fill("2020-02-02")

2.2 复选框与单选框

  • 使用 locator.set_checked() 检查/取消复选框或单选框。
page.get_by_label('I agree to the terms above').check()
expect(page.get_by_label('Subscribe to newsletter')).to_be_checked()

2.3 下拉选择

  • 使用 locator.select_option() 选择单项或多项。
page.get_by_label('Choose a color').select_option('blue')
page.get_by_label('Choose multiple colors').select_option(['red', 'green', 'blue'])

2.4 鼠标点击

  • 支持单击、双击、右击、带修饰键点击、悬停、指定坐标点击。
page.get_by_role("button").click()
page.get_by_text("Item").dblclick()
page.get_by_text("Item").click(button="right")
page.get_by_text("Item").hover()
  • 可强制点击(绕过可操作性检查):
page.get_by_role("button").click(force=True)
  • 也可用 dispatch_event 进行编程式点击:
page.get_by_role("button").dispatch_event('click')

2.5 字符输入(逐字)

  • 用于特殊键盘处理场景,逐字输入:
page.locator('#area').press_sequentially('Hello World!')

2.6 键盘快捷键

  • 使用 locator.press() 输入快捷键或特殊按键。
page.get_by_text("Submit").press("Enter")
page.get_by_role("textbox").press("Control+ArrowRight")
page.locator('#name').press('Shift+A')

2.7 文件上传

  • 使用 locator.set_input_files() 上传单个/多个文件或目录。
page.get_by_label("Upload file").set_input_files('myfile.pdf')
page.get_by_label("Upload files").set_input_files(['file1.txt', 'file2.txt'])
page.get_by_label("Upload directory").set_input_files('mydir')
page.get_by_label("Upload file").set_input_files([])  # 清空
  • 监听文件选择器事件:
with page.expect_file_chooser() as fc_info:
    page.get_by_label("Upload file").click()
file_chooser = fc_info.value
file_chooser.set_files("myfile.pdf")

2.8 聚焦元素

  • 使元素获得焦点:
page.get_by_label('password').focus()

2.9 拖拽操作

  • 快速拖拽:
page.locator("#item-to-be-dragged").drag_to(page.locator("#item-to-drop-at"))
  • 手动拖拽(更精细控制):
page.locator("#item-to-be-dragged").hover()
page.mouse.down()
page.locator("#item-to-drop-at").hover()
page.mouse.up()

2.10 滚动操作

  • Playwright 通常自动滚动,无需手动。
  • 如需手动滚动:
page.get_by_text("Footer text").scroll_into_view_if_needed()
page.get_by_test_id("scrolling-container").hover()
page.mouse.wheel(0, 10)
page.get_by_test_id("scrolling-container").evaluate("e => e.scrollTop += 100")

3. 思维导图

mindmap
  root((Playwright 输入操作))
    文本输入
      fill()
    复选框/单选框
      set_checked()
    下拉选择
      select_option()
    鼠标点击
      click()
      dblclick()
      hover()
      force/dispatch_event
    字符输入
      press_sequentially()
    快捷键
      press()
    文件上传
      set_input_files()
      expect_file_chooser()
    聚焦
      focus()
    拖拽
      drag_to()
      hover+mouse.down/up
    滚动
      scroll_into_view_if_needed()
      mouse.wheel()
      evaluate()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值