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?
2 Answers
Reset to default 13 +50You 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.