Alert Handling in Selenium Webdriver (Selenium 2) with Java

Alert handling in Selenium WebDriver (selenium 2) with Java

This is what worked for me using Explicit Wait from here WebDriver: Advanced Usage

public void checkAlert() {
try {
WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
} catch (Exception e) {
//exception handling
}
}

How to handle pop-up or alert in selenium?

First that's not popup that's an Iframe.

you can get more information from this link how to handle iframe in selenium

first you need to swich to the iframe.

for that you need to write below code after your Thread.sleep(3000); find the iframe name from your html source.

driver.switchTo().frame("notification-frame-~2514428c7");
driver.findElement(By.xpath("//i[@class='wewidgeticon we_close']")).click();

By using this you can close your html popup.

Alert Handling in Selenium Webdriver

Based on the original question and the subsequent comments, I suspect what you're dealing with is a browser pop up window and not an alert. So this won't work

driver.switchTo().alert().accept();

You need to use window handles

Set<String> handles = driver.getWindowHandles(); 
Iterator<String> windows = handles.iterator();
String parent = windows.next();
String child = windows.next();
driver.switchTo().window(child);
driver.findElement(By.xpath("insert xpath to OK button")).click();
driver.switchTo().window(parent);
//continue with steps on parent window

Note: ensure that you add the required synchronization to the code snippet above

How to handle intermittent alert in Selenium WebDriver?

JavascriptExecutor worked for you. Just take care that you should execute it before clicking the event which invoke alert.

((JavascriptExecutor) driver).executeScript("window.confirm = function(msg) { return true; }");

Note :- do not use it after clicking on event which invoke alert confirmation box. Above code by default set the confirmation box as true means you are accepting/click on ok on all confirmation box on that page if invoked

Hope it will help you :)



Related Topics



Leave a reply



Submit