Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

enter image description here

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?

like image 686
Lucas Nunes Avatar asked Apr 26 '16 20:04

Lucas Nunes


People also ask

How do I close a window using Javascript?

Simply call window. close() and it will close the current window. If it doesn't work... it should always work.

How do you close a dialog box in HTML?

The close() method closes the dialog. Tip: This method is often used together with the show() method.

How do I close the current dialog?

Right-click the icon referring to the dialog box from the Windows taskbar and click “Close”.

How do you close a dialog box by clicking outside?

By default, dialog can be closed by pressing Esc key and clicking the close icon on the right of dialog header. It can also be closed by clicking outside of the dialog using hide method. Set the CloseOnEscape property value to false to prevent closing of the dialog when pressing Esc key.


1 Answers

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.

like image 76
Gokhan Kurt Avatar answered Sep 19 '22 09:09

Gokhan Kurt