简体   繁体   中英

Close dialog window on webpage

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM