There is a jquery pop up that I am facing..I tried with the following code to find out wheather there is a pop up or not but the output shows there are no pop ups.but I can see one.. and I am not able to switch to pop up and perform the actions.can anyone Please suggest what to do.
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);
}
Try this:
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);
}
}
For Jquery Alert it will work
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");
}
}
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.