简体   繁体   中英

How to switch to a new pop-up window using selenium/java

I need to automate the following test: after the login on the web page a new pop-up window (with app) opens and all steps should be done in this new window.

Question: how to code to switch from the current login window to the new pop-up window?

Thank you!

If you want to handle child window then you have to use handles in seleneium, Kindly refer below code:

String parentWindowHandle = driver.getWindowHandle(); // get the current window handle

//Perform action on your parent window 
//Perform clcik() action on your parent window that opens a new window    

for (String winHandle : driver.getWindowHandles()) {

         if(!winHandle.equals(parentWindowHandle))
         {
            driver.switchTo().window(winHandle); // Here yor switching control to child window so that you can perform action on child window
            System.out.println("Title of the new window: " +
            driver.getTitle());
            //code to do something on new window
            System.out.println("Closing the new window...");
            driver.close();
         }

   }   

driver.switchTo().window(parentWindowHandle);
System.out.println("Parent window URL: " + driver.getCurrentUrl());

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.

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