How to handle multiple windows in webdriver


  • QA Engineer

    Scenario: Step-1.Click link from window-1.New window gets opened.Say it as window-2. Step-2.Do action on window-2. Step-3.Close window-2. Step-4.Move focus to window-1 Step-5.Click an element in window-1(Here a New window is getting opened) Step-6.Click link from window-1 again opens another window-say it as WINDOW-2 Step-7.Do actions on WINDOW-2 Step-8.Close window-2 Step-9.Move focus to window-1 again and perform action. Step-10.Close window-1. ---Clicking the element on parent window on step-5 opens in a new window---** String oldTab = driver.getWindowHandle(); //clicking link to open new window if(driver.findElement(By.xpath("element")).isDisplayed()) { Actions aBuilder = new Actions(driver); aBuilder.keyDown(Keys.SHIFT).click(link).perform(); ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles()); newTab.remove(oldTab); driver.switchTo().window(newTab.get(0)); //peform action driver.close(); driver.switchTo().window(oldTab); } String oldTab1 = driver.getWindowHandle(); driver.navigate().refresh(); //Clicking link to open in new Window if(driver.findElement(By.xpath("element")).isDisplayed()) { Actions aBuilder1 = new Actions(driver); aBuilder1.keyDown(Keys.SHIFT).click(link1).perform(); ArrayList<String> newTab1 = new ArrayList<String>(driver.getWindowHandles()); newTab1.remove(oldTab1); driver.switchTo().window(newTab1.get(0)); //perform action driver.close(); driver.switchTo().window(oldTab1); }



  • keyDown method presses the key but doesn't release it. Try to replace aBuilder1.keyDown(Keys.SHIFT).click(link1).perform(); with aBuilder1.keyDown(Keys.SHIFT).click(link1).perform(); aBuilder1.keyUp(Keys.SHIFT).perform();



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2