Want to open new tabs and switch between them


I have a scenario in which I need to open 2 tabs in the same browser and then perform actions that will run in a loop.

After the loop (set of actions) is complete then close the browser.

Is it possible to open 1st tab -> perform respective actions -> then open 2nd tab and perform  respective actions -> repeat these steps around 20 time s and then close it ????

som Train Asked on January 15, 2015 in Selenium WebDriver.
Add Comment
2 Answer(s)

Switching tab is not the official supported scenario by Selenium because each tab has same handle and its not possible to distinguish between the two.

But the truth of the matter is two tabs is same as two windows in terms of your application testing. So have two windows opened up instead of two tabs. Then you can follow this tutorial



Virender Singh Professor Answered on January 21, 2015.
Add Comment

// 1) Launch new Browser, create an instance of FirefoxDriver.
driver = new FirefoxDriver();

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

// 2) Open URL

// 3) Get Window name (Use GetWindowHandle command)
// To get the window handle of the current window.
String handle = driver.getWindowHandle();//Return a string of alphanumeric window handle


// 4) Click on Button “New Message Window”, it will open a Pop Up Window
driver.findElement(By.name(“New Message Window”)).click();

// Store and Print the name of all the windows open, get all windows
Set<String> handles = driver.getWindowHandles();

// Pass a window handle to the other window
for(String handle1 : driver.getWindowHandles())
System.out.println(“Window Handle: ” + handle1);

// Store and Print the name of all the windows open
Iterator<String> iteratorP = handles.iterator();
while (iteratorP.hasNext()) {
System.out.println(“Window Handle: ” + iteratorP.next());

// 6) Close the Pop Up Window (Use Switch Command to shift window)

driver.switchTo().window(handle); // switch back to parent window

//Close the browser

pradhakrishna284 Train Answered on January 22, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.