Splash与confirm()模态弹窗交互问题及显式处理方法咨询
使用Splash处理页面Confirm模态对话框的问题
关于弹窗被隐式关闭的确认
你的推测是对的:Splash默认会自动处理JavaScript的confirm、alert、prompt这类模态对话框。对于confirm弹窗,Splash默认会自动返回true(模拟点击「确定」按钮),弹窗会被隐式关闭,这就是你后续截图看不到弹窗的原因。
如何显式观察并处理Confirm弹窗
你可以通过注入自定义JavaScript重写window.confirm方法,实现拦截弹窗、记录内容并手动控制关闭操作:
示例Lua脚本
function main(splash, args) -- 注入JS重写confirm方法,拦截弹窗并记录内容 splash:runjs([[ window.originalConfirm = window.confirm; window.confirm = function(message) { // 将弹窗内容存入全局变量,供后续获取 window.confirmDialogContent = message; // 手动控制返回值:true=模拟点击确定,false=模拟点击取消 return true; }; ]]) -- 加载目标页面 assert(splash:go(args.url)) -- 等待页面加载或触发弹窗的操作完成 assert(splash:wait(3)) -- 获取拦截到的弹窗内容 local dialog_content = splash:evaljs("window.confirmDialogContent || '未触发confirm弹窗'") print("捕获到的confirm弹窗内容:", dialog_content) -- 返回结果(包含截图和弹窗内容) return { png = splash:png(), confirm_message = dialog_content } end
额外说明
- 如果需要根据弹窗内容动态决定是否确认,可以在重写的
confirm方法中添加逻辑判断(比如判断message内容后返回对应布尔值) - 不建议通过
--disable-javascript-dialogs启动参数禁用Splash的自动处理,因为未处理的模态弹窗会阻塞页面加载流程
内容的提问来源于stack exchange,提问作者Alex Iliev
相关产品推荐
相关产品推荐

