How to auto press ok on javascript alert - javascript

I want to code the javascript alert so when a message is displayed, the OK button is automatically pressed.
HTML
<html>
<script language=javascript>
function rdir()
{
alert("Please wait while the page is redirected!");
window.location="/alarms_act_lat.htm";
}
</script>
<body onLoad=rdir()>
<center>
<h1>LATCHED ALARMS ARE CLEARED</h1>
</center>
</body>
</html>

That's not possible – at least, not without a browser extension. All JavaScript execution in the browser stops during alert().

Related

I need to click on 'OK' buttom from ALERT on Internet Explorer with Selenium

I need to click on 'OK' Buttom from Alert on Internet Explorer. I tried use drive.switch_to.alert().accept()/.send_keys(Keys.ENTER) on Selenium Webdrive with Python, but it's not working.
How can I focus on this alert and do something to close it.
The alert happens before click on this button:
<input name="btn_gerar" id="btn_gerar" type="button" onclick="geraRelatorio()" value="Gerar relatório" class="botao" style="">
I suggest you first move the control from the web page to the alert popup and then try to accept it. That way it is working fine on my side.
HTML code:
<!DOCTYPE html>
<html>
<head>
<title>demo</title>
</head>
<body>
<input name="btn_gerar" id="btn_gerar" type="button" onclick="geraRelatorio()" value="Gerar relatório" class="botao" style="">
<script>
function geraRelatorio() {
alert("Hi!, I am a Simple Alert. Please Click on the 'OK' Button.");
}
geraRelatorio();
</script>
</body>
</html>
Python code:
from selenium import webdriver
driver = webdriver.Ie(executable_path='Path_to_web_driver...\\IEDriverServer.exe')
driver.get('https://Your_website_URL_here...')
driver.implicitly_wait(1)
alert_obj = driver.switch_to.alert
alert_obj.accept()
#driver.quit()
Output:
Make sure that code to close the alert() get execute after alert() is displayed on the screen. Sometimes code gets executed first and after that alert() gets display so the user thinks that his code is not working.

Logout once the Browser is Closed

I'm trying to record user sign out time in mysql DB once he close the browser. I searched for it but the most articles talking about window.onbeforeunload JavaScript event, but doesn't work with all browsers. even it works with F5 button can't specify massage or action,.
<!DOCTYPE html>
<html>
<body onbeforeunload="return myFunction()">
<p>Close this window, press F5 or click on the link below to invoke the onbeforeunload event.</p>
test by redirecting
<script>
function myFunction() {
return "specific massage";
}
</script>
</body>
</html>

In Javascript setting a textarea with focus() does not work if called as a child window

I have a simple test page that sets the focus to a textarea on an oninit function. However the exact code fails to do this if the page is called as a child.
Putting alert box proves that the oninit function is being called but fails to put the focus in the textbox. Pressing reload though does then focus correctly.
So given that my code works perfectly when called on a main page, and also works in a child if reload is called, then why doesn't it work the first time?
<html>
<body onload="init()">
<script type="text/javascript">
function init()
{
document.getElementById("message").focus();
}
</script>
<textarea id="message" rows=10 cols=40></textarea>
</body>
</html>
Nothing clever here as you can, just only doesn't work if the page is loaded by window.open("test2.html");
You can also use setTimeout irrespective of the event.
setTimeout(function() {
document.getElementById("elementId").focus();
}, 0);
which browser do you use?
I check in firefox, chrome & IE.
Your code runs perfect and focus on the textarea.
I create two file test1.html and test2.html in a same directory.
In test1.html i insert the the follwing code..
<html>
<body>
<script type="text/javascript">
function init()
{
window.open('test2.html');
}
</script>
<button onclick="init()">test</button>
</body>
</html>
And in test2.html..
<html>
<body onload="init()">
<script type="text/javascript">
function init()
{
document.getElementById("message").focus();
}
</script>
<textarea id="message" rows=10 cols=40></textarea>
</body>
</html>
Than, I run the test1.html and click the button and test2.html appears with focus on the textarea.

async attribute in script tag in Firefox

I'm trying to test the following code in Firefox 13.0.1, but it doesn't work correctly:
<!DOCTYPE html>
<html>
<head>
<title>Exercise 1</title>
<script type="text/javascript" async src="example1.js"></script>
<script type="text/javascript" async src="example4.js"></script>
<script type="text/javascript" src="example3.js"></script>
</head>
<body>
<p>Hello world</p>
</body>
</html>
//example1.js:
alert("I'm the example 1");
//example3.js
alert("I'm the example 3");
//example4.js
alert("I'm the example 4");
When I open the file which contains the above html code, the following occurs:
I EDIT THE PROCESS I FOLLOW
In first place, the script "example3.js" is executed, and then a pop-up with the text "I'm the example 3" appears. Afterwards, I click on OK button inside the pop-up window.
In second place, the script "example4.js" is executed, and then a pop-up with the text "I'm the example 4" appears. Afterwards, I click on OK button inside the pop-up window.
In third place, the script "example1.js" is executed, and then a pop-up with the text "I'm the example 1" appears. Afterwards, I click on OK button inside the pop-up window.
And finally, the body content should be displayed (he paragraph with the text "Hello World"), but it doesn't in Firefox, but in Chrome. In Firefox, the page load doesn't stop.
If I open Firebug in Firefox after last script was executed, I realize that the browser does not receive the body element.
I don't know if my code is correct or is a bug from Firefox.
Thank you.
This is an already reported bug in firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=692754

JQuery - Write to opener window

I have an HTML page that opens another page via JavaScript. When a user clicks a button in the other page, I want to post a message in a DIV of the opening page via JQuery. I cannot put my finger on it, but I cannot seem to get this to work. Here is my opener page
<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
</head>
<body>
<input type="button" onclick="window.open('dialog.html', '_blank', 'height=200, width=300');" value="launch!" />
<div id="testDiv"></div>
</body>
</html>
When the user clicks the "launch!" button, a dialog will appear. The code for the dialog looks like this:
<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
</head>
<body>
<input type="button" onclick="updateOpener()" value="Update Opener" />
<script type="text/javascript">
function updateOpener()
{
var testDiv = window.opener.jQuery("#testDiv");
if (testDiv != null) {
alert("here");
testDiv.html("Updated!");
}
}
</script>
</body>
</html>
Surprisingly, the alert box appears. However, I cannot seem to update the HTML of the DIV in my opening page. Does anyone know how to do this?
You're referencing "confirmDiv". Where is that DIV?
You can't do that if the parent page (the opener) resides on another domain. Otherwise, your code works perfectly.
Also, your != null check is probably not doing what you think it is doing, as the jQuery function never returns null. If you are checking for the existence of an element, you need to do it this way...
var el = $("#myElementId");
if(el.length == 0)
alert('Not found!');
Ummm, it works for me in Firefox 3.0.11, IE8 and Chrome 2... (I.e. the dialog.html button updates the HTML in the opener page to say 'Updated!'.)
Oddly, your example works fine for me in Chrome, IE 8 and FireFox. Do you have any other details?

Categories