简体   繁体   English

如何使用Java切换到Jquery Pops Inselenium WebDriver

[英]How to switch to Jquery pop ups inselenium webdriver using java

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. 我面对的是一个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);
    }

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 对于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.

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