最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Close dialog window on webpage - Stack Overflow

programmeradmin2浏览0评论

I need to trigger some actions inside someone else's webpage. I have this code so far:

IHTMLElementCollection DeleteCollection = 
    (IHTMLElementCollection)myDoc.getElementsByTagName("a");

foreach (HTMLAnchorElement buttonDelete in DeleteCollection){
    if (buttonDelete.title != null && buttonDelete.title.StartsWith("Delete")){
        buttonDelete.click();
            // problem goes here
        myDoc.activeElement.click(); 
        SendKeys.Send("{ENTER}");
    }
}

This dialog pops up:

I tried myDoc.activeElement.click(); and SendKeys.Send("{ENTER}"); but the dialog seems to be out of the page, so I don't know how to trigger the OK button. How can I close the window?

I need to trigger some actions inside someone else's webpage. I have this code so far:

IHTMLElementCollection DeleteCollection = 
    (IHTMLElementCollection)myDoc.getElementsByTagName("a");

foreach (HTMLAnchorElement buttonDelete in DeleteCollection){
    if (buttonDelete.title != null && buttonDelete.title.StartsWith("Delete")){
        buttonDelete.click();
            // problem goes here
        myDoc.activeElement.click(); 
        SendKeys.Send("{ENTER}");
    }
}

This dialog pops up:

I tried myDoc.activeElement.click(); and SendKeys.Send("{ENTER}"); but the dialog seems to be out of the page, so I don't know how to trigger the OK button. How can I close the window?

Share Improve this question edited May 6, 2016 at 14:31 4444 3,68010 gold badges34 silver badges46 bronze badges asked Apr 26, 2016 at 20:53 Lucas NunesLucas Nunes 1836 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 13 +50

You cannot close a browser dialog programatically. What you can do is hijack browser popup behavior. This might work for you:

window.confirm=function(){ return true; };
window.alert=function(){ return true; };
window.prompt=function(){ return "textOfMyChoice"; };

Basically, insert this javascript before clicking your buttons. If you want to later restore the popup behavior store them in another global variable.

I suppose, any anchor tag in your example has a javascript native confirm dialog. You don't close programmatically this dialog.

But you can change javascript for all anchor element and substitute confirm dialog with your custom jQuery dialog. this way allow you to have full control on this dialog: close, hide, set timeout etc etc.

Transform

<a href="..." onclick="return confirm('are you sure?')" ... >...</a>

to

<a href="..." onclick="return openMyjQueryDialog('are you sure?')" ... >...</a>

So, user can continue with normal behavior, your script can take full control on all dialog element.

发布评论

评论列表(0)

  1. 暂无评论