Python的Playwright - 1.3 Dialog

本文介绍如何使用Playwright处理浏览器中的对话框事件,包括alert、prompt、confirm及beforeunload等类型的对话框。通过实例展示了如何监听这些对话框,并正确地接受或取消它们,避免页面冻结。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dialog对象用来侦听对话框操作,alertbeforeunload(在文档即将被卸载之前发生此事件),confirmprompt

官方解释:Dialogs are dismissed automatically, unless there is a page.on(“dialog”) listener. When listener is present, it must either dialog.accept(**kwargs) or dialog.dismiss() the dialog - otherwise the page will freeze waiting for the dialog, and actions like click will never finish.

也就是说对话框如果没有page.on('dialog')监听会自动消失,一旦有了监听事件,必须后面跟着dialog.accecpt(**kwargs)dialog.dismiss(),否则对话框会一直停在点击事件,如下:


alert提示框:

from playwright.sync_api import sync_playwright

with sync_playwright() as sp:
    browser = sp.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    # 代码实现
    page.on('dialog', lambda dialog: print(dialog.message))  # hello playwright!
    page.evaluate('alert("hello playwright!")')
    context.close()
    browser.close()

结果如下:
alert弹窗

除非手动点击,否则会一直阻塞,加入dialog.accecpt(**kwargs)dialog.dismiss()即可解决上述问题

from playwright.sync_api import sync_playwright


def handle_dialog(dialog):
    print(dialog.message)
    dialog.accept()


with sync_playwright() as sp:
    browser = sp.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    # 代码实现
    page.on('dialog', handle_dialog)
    page.evaluate('alert("hello playwright!")')
    context.close()
    browser.close()

prompt提示框:

from playwright.sync_api import sync_playwright


def handle_dialog(dialog):
    print(dialog.message, dialog.type, dialog.default_value)
    dialog.accept(prompt_text='OK')


with sync_playwright() as sp:
    browser = sp.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    # 代码实现
    page.on('dialog', handle_dialog)
    page.evaluate('prompt("hello playwright!","good idea")')
    context.close()
    browser.close()

prompt()有两个参数:

  • text: 要在对话框中显示的纯文本
  • defaultText: 默认的输入文本
    prompt弹窗

上述使用了dialog.accept(prompt_text=‘OK’): [ prompt_text 官方解释: A text to enter in prompt. Does not cause any effects if the dialog’s type is not prompt. Optional.]不过并没有发现prompt_text参数对实际操作有什么影响

dialog.default_value: 如果对话框是prompt,则其值为defaultText

dialog.message: 对话框中显示的消息

dialog.type: 对话框的类型,alertbeforeunloadconfirmprompt


confirm提示框可以自行演示


beforeunload提示框:

用脚本模拟的方式弹出beforeunload提示框:

from playwright.sync_api import Playwright, sync_playwright


def handle_dialog(dialog):
    print(dialog.type)
    dialog.dismiss()
    

def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()

    # Open new page
    page = context.new_page()
    page.goto("https://www.w3school.com.cn/tiy/t.asp?f=event_onbeforeunload")
    # Click text=点击此处访问 w3school.com.cn
    page.on("dialog", handle_dialog)
    page.frame(name="iframeResult").click("text=点击此处访问 w3school.com.cn")

    # ---------------------
    context.close()
    browser.close()


with sync_playwright() as playwright:
    run(playwright)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值