Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium automatically accepting alerts

Does anyone know how to disable this? Or how to get the text from alerts that have been automatically accepted?

This code needs to work,

driver.findElement(By.xpath("//button[text() = \"Edit\"]")).click();//causes page to alert() something
Alert alert = driver.switchTo().alert();
alert.accept();
return alert.getText();

but instead gives this error

No alert is present (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 2.14 seconds

I am using FF 20 with Selenium 2.32

like image 326
Zackkenyon Avatar asked May 08 '13 18:05

Zackkenyon


People also ask

Can Selenium handle alerts?

To reduce human intervention and ease this task, Selenium provides a wide range of functionalities and methods to handle alerts. The following methods are useful to handle alerts in selenium: 1. Void dismiss(): This method is used when the 'Cancel' button is clicked in the alert box.

Can we automate pop up in Selenium?

Handling these alerts in Selenium is a little tricky and beyond the WebDriver's capabilities, as Selenium is an automation testing tool for web applications only, and we need third party utility to automate window based popups. A few of those utilities are AutoIT and Robot Class in Java.

Can Selenium handle window pop ups?

Yes, it is possible to handle Windows based pop-ups in Selenium webdriver. Sometimes on clicking a link or a button, another window gets opened. It can be a pop up with information or an advertisement. The methods getWindowHandles and getWindowHandle are used to handle child windows.


2 Answers

Just the other day i've answered something similar to this so it's still fresh. The reason your code is failing is if the alert is not shown by the time the code is processed it will mostly fail.

Thankfully, the guys from Selenium WebDriver have a wait already implemented for it. For your code is as simple as doing this:

String alertText = "";
WebDriverWait wait = new WebDriverWait(driver, 5);
// This will wait for a maximum of 5 seconds, everytime wait is used

driver.findElement(By.xpath("//button[text() = \"Edit\"]")).click();//causes page to alert() something

wait.until(ExpectedConditions.alertIsPresent());
// Before you try to switch to the so given alert, he needs to be present.

Alert alert = driver.switchTo().alert();
alertText = alert.getText();
alert.accept();

return alertText;

You can find all the API from ExpectedConditions here, and if you want the code behind this method here.

This code also solves the problem because you can't return alert.getText() after closing the alert, so i store in a variable for you.

like image 158
aimbire Avatar answered Oct 17 '22 15:10

aimbire


Before you accept() the alert you need to get the text. What you're doing right now is accepting (clicking "OK") on the alert then trying to get the alerts text after it's out of the screen, i.e. no alert present.

Try the following, I just added a String that retrieves the alert text then return that string instead.

driver.findElement(By.xpath("//button[text() = \"Edit\"]")).click();//causes page to
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
alert.accept();
return alertText;
like image 2
so cal cheesehead Avatar answered Oct 17 '22 15:10

so cal cheesehead