简体   繁体   English

Selenium Webdriver中的警报处理

[英]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. 我试图切换到弹出警报并单击“确定”按钮,但是出现一个错误,提示未找到xpath(用于“确定”按钮)。

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 注意:确保将所需的同步添加到上面的代码段中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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