前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >解锁Playwright新技能:掌握click()方法,让测试更高效

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

作者头像
小博测试成长之路
发布2024-12-06 11:22:29
发布2024-12-06 11:22:29
19200
代码可运行
举报
文章被收录于专栏:软件测试学习软件测试学习
运行总次数:0
代码可运行

学习笔记

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

看源码里面的描述如下:

代码语言:javascript
代码运行次数:0
运行
复制
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.

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

代码语言:javascript
代码运行次数:0
运行
复制
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
    # 秒),直到导航或动作完成。这确保了在执行后续操作之前,页面已经完成了所有相关的异步操作。


End
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-07-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小博测试成长之路 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档