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,370 points
5,391 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,140 points
9,242 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,490 points
4,476 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 6, 2020 in Selenium by Sri
• 3,190 points
16,034 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,500 points
13,674 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,190 points
9,143 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,910 points
7,221 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 3, 2020 in Selenium by Sri
• 3,190 points
7,875 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
1,018 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP