[英]How to handle windows pop ups using selenium 2.0 (webdriver)?
[英]How to switch to Jquery pop ups inselenium webdriver using java
我面对的是一个jQuery弹出窗口。我尝试使用以下代码来查找是否存在弹出窗口,但输出显示没有弹出窗口。但是我可以看到一个弹出窗口。可以切换到弹出窗口并执行操作。任何人都可以提出建议。
public static boolean runScript(){
JavascriptExecutor js = (JavascriptExecutor) driver;
return (Boolean) js.executeScript("return jQuery.active==0;");
}
public static void FocusOnWindow() throws Exception{
int i=0;
do {
if(!runScript()){
System.out.println("Popup exists");
i++;
}else{
i=5000;
System.out.println("Popup does not exists");
}
}while(i<5000);
}
尝试这个:
Alert alert = driver.switchTo().alert();
alert.accept();
I got the solution.sharing the code.hope it help others.
String parentHandle = driver.getWindowHandle();
String popupWindowHandle = null;
driver.findElement(By.xpath("//a[@id='btnAddProduct']")).click();
Thread.sleep(10000L);
for (String winHandle : driver.getWindowHandles()) {
if(!WinHandle.equals(parentHandle){
popupWindowHandle = WinHandle;
driver.switchTo().window(popupWindowHandle);
}
}
对于Jquery Alert,它将起作用
public void testJQueryAlertHandling() {
WebDriverWait jQueryWait = new WebDriverWait(driver, 2);
try{
jQueryWait.until(ExpectedConditions.presenceOfElementLocated(By.id("popup_container")));
WebElement popup = driver.findElement(By.id("popup_container"));
WebElement message = popup.findElement(By.id("popup_message"));
assertEquals("Alert Message", message.getText());
popup.findElement(By.id("popup_ok")).click();
System.out.println("Alert Present");
}
catch (Exception e){
System.out.println("No alert Present");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.