Here i need to get message that is displayed on alert box and click ok using javascript.
for example : alert("hello");
above that code displays message as hello which is coming from an other website I need to capture that message using javascript and script to get message and hit button.
you can use this way:
function captureAlert() {
// get all scripts
var elem = document.scripts;
// loop and check
for (var i = 0, len = elem.length; i < len; i++) {
var txt = elem[i].textContent.match(/alert\(['"]([^'"]+)['"]\)/);
if (txt) { return txt[1]; } // if matched, return the alert value
}
}
You can just overwrite alert function.
var fn = alert;
alert = function(text) {
document.write('<b>'+text+'</b>')
alert = fn;
}
<button onclick="alert('hello123')">Click</button>