简体   繁体   中英

Alert Handling in Selenium Webdriver

I tried to switch to popup alert and click on OK button, but i got an error saying that xpath (for OK button) is not found.

But this is working for me sometimes using the same code. Could anyone help me out on this. I tried all possible ways that is available in blogs. But i couldn't make it

you need to move control to your pop-up window first before doing any operation on pop-up window:-

below code is to move selenium control on pop-up window

driver.switchTo().alert();

by writing below line

alert.accept();

alert will get close

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM