如何自动点击确认框? [英] How to automatically click a confirm box?

查看:311
本文介绍了如何自动点击确认框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本会点击网站上的图片。该图像有一个锚 href 和一个 onclick href ,但onclick href有一个确认框,一旦被点击就弹出。

onclick HTML是:

My script clicks an image on a site. The image has an anchor href and an onclick href, but the onclick href has a confirm box that pops up once it's clicked.
The onclick HTML is:

onClick="this.href='link2';if (!confirm('Are you sure?')) { return false; }

如果弹出窗口,如何让脚本点击该确认框中的 确定

How do I get the script to click OK in that confirm box, once it pops up?

我正在使用此功能点击图片链接:

I'm using this function to click the picture link:

function click(elm) {
 var evt = document.createEvent('MouseEvents');
 evt.initMouseEvent('click', true, true, window, 0, 1, 1, 1, 1, false, false, false, false, 0, null);
 elm.dispatchEvent(evt);
}
click(picture element)


推荐答案

您可以覆盖确认方法暂时使其静音并返回 true 。下面的示例将使确认无声对话框只有一次,并且只有在它被调用时。一旦执行了沉默对话框,它将恢复其原始功能,随后对 confirm 方法的调用将正常工作。

You can override the confirm method to temporarily silence it and return true. Below example will silence a confirm dialog only once, and only if it's called. Once the silenced dialog is executed, it will restore its original functionality and subsequent call to confirm method will work as normal.

var realConfirm=window.confirm;
window.confirm=function(){
  window.confirm=realConfirm;
  return true;
};
click(picture element);

这篇关于如何自动点击确认框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆