0

I have opened the website and applied the Login then popup window opens, i want to click from window popup but i am not able to switch on popup.

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

I am not able click on popup element at line 5. can you check the code.

user2384525
  • 1
  • 1
  • 6

1 Answers1

0

Check the accepted answer for a similar question

How to handle Pop-up in Selenium WebDriver using Java

You need to - getWindowHandles - & then iterate over them.


Here is the working solution in case you still haven't figured it out (this is for the HDFC example)...

    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...");
    }
Community
  • 1
  • 1
DRVaya
  • 443
  • 4
  • 13
  • Here, Only single popup window is opening so i don't think - we should use the getWindowHandles – user2384525 Mar 12 '15 at 14:24
  • Nopes..... There's a difference between getWindowHandle() and getWindowHandles(). Read this post to get the difference http://stackoverflow.com/questions/25871042/selenium-webdriver-getwindowhandle-method As for your example, use getWindowHandles() ....Give it a shot and see the difference in behaviour. – DRVaya Mar 12 '15 at 14:36
  • Agreed but in above code - only one window is opening so I may be used getWindowHandle() and it should be work. Please open the hdfcbank.com and click on login button – user2384525 Mar 12 '15 at 15:51
  • When you open a browser page, the window handle of the first page (homepage) is returned by getWindowHandle. When you click on login link it opens a popup which is basically another webdriver object (for us a popup), the window Handle of which can be obtained in getWindowHandles. When a popup is spawned by the parent browser process, its handles are available inside getWindowHandles. Please attempt this approach and let me know. – DRVaya Mar 13 '15 at 05:14
  • Check my above answer for a working solution to your query. What you need to understand is getWindowHandles provides a **Set** (unique values) of window handles that is spawned by the WebDriver & it will include the parent browser too along with the child popups. Sorry for reiterating this over again, but I felt you did not understand the fundamental purpose of using getWindowHandles(). – DRVaya Mar 13 '15 at 06:33
  • Did you give this a try ? – DRVaya Mar 17 '15 at 09:07