javascript 如何自动点击确认框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11570868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to automatically click a confirm box?
提问by Frozsht
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:
我的脚本单击了站点上的图像。该图像有一个锚点href
和一个onclick
href
,但 onclick href 有一个确认框,点击后会弹出。
onclick HTML 是:
onClick="this.href='link2';if (!confirm('Are you sure?')) { return false; }
How do I get the script to click OKin 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)
回答by Jay
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.
您可以覆盖该confirm
方法以暂时使其静音并返回true
。下面的示例只会使确认对话框静音一次,并且仅在调用它时才静音。执行静音对话框后,它将恢复其原始功能,随后对confirm
方法的调用将正常工作。
var realConfirm=window.confirm;
window.confirm=function(){
window.confirm=realConfirm;
return true;
};
click(picture element);