Java:在窗口处理期间焦点不在pop窗口上

时间:2015-03-12 12:19:51

标签: selenium-webdriver webdriver

我已打开网站并应用登录然后弹出窗口打开,我想点击窗口弹出窗口,但我无法打开弹出窗口。

    driver.get("https://hdfcbank.com/");
    driver.findElement(By.id("loginsubmit")).click();   

    String loginWindow = driver.getWindowHandle();
    driver.switchTo().window(loginWindow);  

    driver.findElement(By.xpath("//*[@id='wrapper']/div[6]/a/img")).click();

我无法点击第5行的弹出元素。你可以查看代码。

1 个答案:

答案 0 :(得分:0)

检查已接受的答案以获得类似问题

How to handle Pop-up in Selenium WebDriver using Java

你需要 - getWindowHandles - &然后迭代它们。


以下是您仍未解决的工作解决方案(这适用于HDFC示例)......

    String test_URL = "http://www.hdfcbank.com/";
    String css_login = "img#loginsubmit";
    String css_popup_continue = "img[alt='Continue']";

    browser = new FirefoxDriver();
    browser.navigate().to(test_URL);

    List<WebElement> objLogin = browser.findElements(By.cssSelector(css_login));
    if (objLogin.size() > 0) {

        objLogin.get(0).click();

        String parentWindowHandle = browser.getWindowHandle(); // save the current window handle.
        WebDriver popup = null;
        Iterator<String> windowIterator = browser.getWindowHandles().iterator();
        while(windowIterator.hasNext()) {
            String windowHandle = windowIterator.next();
            popup = browser.switchTo().window(windowHandle);
            if (popup.getTitle().contains("NetBanking")) {
                List<WebElement> objPopupElement = popup.findElements(By.cssSelector(css_popup_continue));
                if(objPopupElement.size() > 0){
                    System.out.println("Switched to Popup and found element...");
                    objPopupElement.get(0).click();

                    //Do any other operations...
                    break;
                }
            }
        }
        //always safe to switch back to parent window to avoid any null pointers, unless parent process got closed...
        browser.switchTo().window(parentWindowHandle);
    }
    else {
        System.out.println("Logon button not found...");
    }