学习笔记
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.
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
# 秒),直到导航或动作完成。这确保了在执行后续操作之前,页面已经完成了所有相关的异步操作。
End