[英]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.