How to apply wait for new window using Selenium WebDriver?

0 votes
How to apply wait for new window using Selenium WebDriver?
Jul 8 in Selenium by Manav
87 views

1 answer to this question.

0 votes

As sometimes during web automation, when you perform click on a particular hyper link, it opens up a new window. In this post I will explain how to apply wait for a new window until it gets open as-

public class WaitForNewWindow {

 @Test

public void testApp() {

     WebDriver driver = new ChromeDriver();

     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    driver.get("http://automate-apps.com/contents/");

    driver.findElement(By.id("why-selenium")).click();

     //Verify new window is opened or not

    Assert.assertTrue(waitForNewWindow(driver,10), "New window is not opened");

     System.out.println("New window has been opened");

     driver.quit();

    }

  public boolean waitForNewWindow(WebDriver driver, int timeout){

          boolean flag = false;

          int counter = 0;

          while(!flag){

              try {

                  Set<String> winId = driver.getWindowHandles();

                  if(winId.size() > 1){

                      flag = true;

                      return flag;

                  }

                  Thread.sleep(1000);

                  counter++;

                  if(counter > timeout){

                      return flag;

                  }

              } catch (Exception e) {

                  System.out.println(e.getMessage());

                  return false;

              }

          }

          return flag;

   }

}

answered Jul 9 by Vaishali Walia

Hi Vaishali, that's an exactly copied answer from here: http://automate-apps.com/how-to-apply-wait-for-new-window-using-selenium-webdriver/

Can you share any other answer of your own to explain the answer better?

Related Questions In Selenium

0 votes
1 answer

Wait for an element to be accessible using Selenium WebDriver

You can try out the below code: ...READ MORE

answered May 18, 2018 in Selenium by Atul
• 10,240 points
120 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
3,314 views
0 votes
1 answer

How to I switch to a new window for links with “blank” targets using Capybara?

Capybara 2.3 includes the new window management ...READ MORE

answered Jul 11, 2018 in Selenium by Meci Matt
• 9,420 points
387 views
0 votes
5 answers
0 votes
1 answer

How to wait for the webpage to load in selenium using java

Firstly, when you launch a web application, ...READ MORE

answered Aug 28, 2018 in Selenium by bug_seeker
• 15,350 points
165 views
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,090 points
548 views
0 votes
1 answer

Impilicit wait vs Explicit wait vs Fluent wait

Implicit wait: Your telling the WebDriver the ...READ MORE

answered Apr 13, 2018 in Selenium by king_kenny
• 3,650 points
824 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
882 views