声明:文章中引用的视频为微信群里面的山豆根大佬原创所录制哟~免费视频录制剪辑不易,请大家多多支持。

并且,大佬为这一系列的视频创作还专门购买了服务器搭建了一个实战项目和练习元素定位的网站。网站的具体信息可参考我之前的文章喔:

推荐一个自动化测试练习网站

学习笔记

解锁Playwright新技能:掌握click()方法,让测试更高效_视频录制

click() 方法不就是元素点击操作吗?这有什么好讲的?

看源码里面的描述如下:

This method clicks the element by performing the following steps:
1. Wait for [actionability](https://playwright.dev/python/docs/actionability) checks on the element, unless `force` option is set.
1. Scroll the element into view if needed.
1. Use `page.mouse` to click in the center of the element, or the specified `position`.
1. Wait for initiated navigations to either succeed or fail, unless `noWaitAfter` option is set.


If the element is detached from the DOM at any moment during the action, this method throws.


When all steps combined have not finished during the specified `timeout`, this method throws a `TimeoutError`.
Passing zero timeout disables this.
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

解锁Playwright新技能:掌握click()方法,让测试更高效_服务器搭建_02

click方法有很多可选的参数 ,可以通过下面的代码可以做个简单的了解:




def test_pw_click(page: Page):
    page.goto("/demo/button")
    page.get_by_text("点击我试试1").click(
        modifiers=["Control"]
    )  # 在点击的时候添加其他按键一起点击
    page.get_by_text("点击我试试1").click(
        position={"x": 10, "y": 15}
    )  # 根据坐标选择点击位置(位置不能超过元素的宽度和高度),可以根据元素的.bounding_box()方法获取元素的大小
    page.get_by_text("点击我试试1").click(
        force=True
    )  # 强制点击,如果元素不可见,也可以点击
    page.get_by_text("点击我试试1").click(
        button="right"
    )  # 右键点击 ,可选值 "left", "middle", "right"
    page.get_by_text("点击我试试1").click(
        click_count=3, delay=1_000
    )  # click_count:点击次数,delay:延迟时间 指定在点击之前等待的时间(以毫秒为单位)
    page.get_by_text("点击我试试1").click(
        no_wait_after=True
    )  # no_wait_after:如果 noWaitAfter 设置为 true,则 Playwright 在执行操作后不会等待任何导航或动作完成。这意味着,如果点击操作触发了页面导航或其他异步事件,Playwright
    # 不会等待这些事件完成,而是立即继续执行后续的命令。如果 noWaitAfter 设置为 false(默认值),Playwright 会在执行操作后等待默认的时间(通常是 30
    # 秒),直到导航或动作完成。这确保了在执行后续操作之前,页面已经完成了所有相关的异步操作。
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

End




关注公众号,测试干货及时送达