Handling a popup window using selenium

0 votes

I have a circumstance in which clicking a linked webpage opens a popup window. And after the popup window opens the focus is in the popup window and master window is disabled. And I am unable to get the control transferred to the popup window. Please have a look at the following code.

driver.findElement(By.linkText("Click me")).click();// when this line of code is reached then a popup window opens.

System.out.println("After Clicking me"); // After the popup window opens this line of code is never executed.

I am unable to transfer the control from parent window to the popup window. I am aware of the following command.

driver.switchTo().window("popup window");
Aug 31, 2018 in Selenium by darklord
• 6,140 points
65 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

This is a code I use when I need to work with a following pop-up window, close it and go back to my main window. Of course, it has been simplified for the purpose of this answer. It maintains a handle of the original window (main) so it can make a difference between the others.

It requires an explicit WebDriverWait because I did have problems during development that code got to run before the window actually got open, so this might not be an ideal condition,

function manipulatePopUp(final WebDriver driver, final WebDriverWait wait) {
    final String mainWindowHandle = driver.getWindowHandle();
    driver.findElement(By.id("linkThatOpensPopUp")).click();

    wait.until(new ExpectedConditions<Boolean>() {
        @Override
        public Boolean apply(WebDriver d) {
            return (d.getWindowHandles().size() != 1);
        }
    });

    for (String activeHandle : driver.getWindowHandles()) {
        if (!activeHandle.equals(mainWindowHandle)) {
            driver.switchTo().window(activeHandle);
        }
    }

    driver.close();
    driver.switchTo().window(mainWindowHandle);
}
answered Aug 31, 2018 by Meci Matt
• 9,400 points

Related Questions In Selenium

0 votes
1 answer

Handling new window using Selenium

You are not switching to new window. ...READ MORE

answered Apr 2, 2018 in Selenium by DragonLord999
• 8,360 points
1,048 views
0 votes
1 answer

Handling calendar popup using Selenium WebDriver

You are selecting wrongly your elements in ...READ MORE

answered Apr 17, 2018 in Selenium by Shubham
• 12,110 points
945 views
0 votes
1 answer

How to open a browser window in full screen using Selenium WebDriver with C#

Simply Use this: driver.Manage().Window.Maximize(); READ MORE

answered Jun 7, 2018 in Selenium by Samarpit
• 5,130 points
1,833 views
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,030 points
56 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
420 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
43 views
0 votes
1 answer

Login popup window using selenium webdriver?

Follow this steps: 1) Open the FireFox browser 2) ...READ MORE

answered Aug 23, 2018 in Selenium by Meci Matt
• 9,400 points
38 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.