How to apply wait for new window using Selenium WebDriver

0 votes
How to apply wait for new window using Selenium WebDriver?
Jul 9, 2019 in Selenium by Manav
9,217 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("https://www.edureka.co/blog/what-is-selenium/");

    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 10, 2019 by Vaishali Walia

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
1,776 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,111 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,460 points
3,903 views
0 votes
8 answers

How to open a link in new tab of chrome browser using Selenium WebDriver?

This below code works for me in ...READ MORE

answered Dec 14, 2020 in Selenium by Gitika
• 65,770 points
103,141 views
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 29, 2018 in Selenium by bug_seeker
• 15,510 points
1,955 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,771 views
0 votes
1 answer

Impilicit wait vs Explicit wait vs Fluent wait

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

answered Apr 14, 2018 in Selenium by king_kenny
• 3,710 points
4,349 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,187 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,263 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