How to switch tabs using Selenium Webdriver - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Tuesday 30 September 2014

How to switch tabs using Selenium Webdriver

Generally it comes the scenario  when on clicking an element in Web Application results in opening a a new tab. 

We can focus on the newly opened tab using the following snippet


String oldTab = driver.getWindowHandle();
   ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());
   newTab.remove(oldTab);

   // change focus to new tab
   driver.switchTo().window(newTab.get(0));
assertEquals("java - Google Search", driver.getTitle());// expected,actual


No comments:

Post a Comment