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 Sahiti
• 6,380 points
2,526 views

1 answer to this question.

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,460 points

Related Questions In Selenium

0 votes
1 answer
0 votes
2 answers

Handling new window using Selenium

You can try something like this too:  ...READ MORE

answered Aug 27, 2019 in Selenium by Abha
• 28,020 points
6,753 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
• 13,480 points
3,100 views
0 votes
2 answers

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

Hi , we have inbuilt method Maximize(). driver.Manage().Wind ...READ MORE

answered Sep 5, 2020 in Selenium by Sri
• 3,010 points
11,642 views
0 votes
2 answers

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,520 points
4,909 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,010 points
5,708 views
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,890 points
3,483 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2, 2020 in Selenium by Sri
• 3,010 points
4,189 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,460 points
277 views