如何使用java切换到selenium webdriver中的Jquery弹出窗口

时间:2014-09-16 10:36:50

标签: java jquery selenium webdriver

我正面临一个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);
    }

3 个答案:

答案 0 :(得分:0)

试试这个:

Alert alert = driver.switchTo().alert();
alert.accept();

答案 1 :(得分:0)

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);
    }
    }

答案 2 :(得分:0)

对于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");
        }        
     }