LambdaTest Community

Find answers, support, and inspiration from other users

Questions
LambdaTest Community
Answered

How can I handle multiple tabs in Selenium using the Window handler method?

Can anyone please explain me how i can handle multiple tabs in Selenium using the Window handler method.

Up Vote Down Vote 0 Votes
Posted 3 months ago

Answers


Handling Tabs In Selenium Using The Window Handler Method

Now, all we need to do is switch to this opened tab using Window Handler methods. Code snippet below for your reference:

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import http://org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class HandlingMultipleTabs {
 public static void main(String[] args) throws InterruptedException, AWTException {
 // TODO Auto-generated method stub
 System.setProperty("webdriver.chrome.driver", ".\\ChromeDriver\\chromedriver.exe");
 WebDriver driver=new ChromeDriver();
 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 //Navigating to airbnb
 driver.get("Vacation Rentals, Homes, Experiences & Places - Airbnb");
 driver.manage().window().maximize();
 String currentHandle= driver.getWindowHandle();
 //locating the location, looking for homestays
 driver.findElement(Domain Premium: By.id("Koan-magic-carpet-koan-search-bar__input")).sendKeys("Goa", Keys.ENTER);
 //Clicking on search button
 driver.findElement(By.xpath("//button[@type='submit']")).click();
 String urlToClick=driver.findElement(By.xpath("//div[text()='Luxury Three Bedroom Apartment with Pool & Jacuzzi']/ancestor::a")).getAttribute("href");
 //opening the new tab
 Robot r = new Robot(); 
 r.keyPress(KeyEvent.VK_CONTROL);
 r.keyPress(KeyEvent.VK_T);
 r.keyRelease(KeyEvent.VK_CONTROL);
 r.keyRelease(KeyEvent.VK_T);
 //getting all the handles currently available
 Set<String> handles=driver.getWindowHandles();
 for(String actual: handles)
 {
  if(!actual.equalsIgnoreCase(currentHandle))
  {
  //switching to the opened tab
  driver.switchTo().window(actual);
  //opening the URL saved.
  driver.get(urlToClick);
  }
 }
 }
}

Use the below command if you wish to switch back to the original tab.

1

driver.switchTo().defaultContent();

Now, let’s try to open the tab using JavascriptExecutor and switch to that tab for the same scenario above. Below is the referenced code snippet:

import java.util.Set;
import java.util.concurrent.TimeUnit;
import http://org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class multipltabsonce123 {
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 System.setProperty("webdriver.chrome.driver", ".\\ChromeDriver\\chromedriver.exe");
 WebDriver driver=new ChromeDriver();
 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 //Navigating to airbnb
 driver.get("Vacation Rentals, Homes, Experiences & Places - Airbnb");
 driver.manage().window().maximize();
 String currentHandle= driver.getWindowHandle();
 //locating the location, looking for homestays
 driver.findElement(Domain Premium: By.id("Koan-magic-carpet-koan-search-bar__input")).sendKeys("Goa", Keys.ENTER);
 //Clicking on search button
 driver.findElement(By.xpath("//button[@type='submit']")).click();
 String urlToClick=driver.findElement(By.xpath("//div[text()='Luxury Three Bedroom Apartment with Pool & Jacuzzi']/ancestor::a")).getAttribute("href");
 //opening the new tab
 ((JavascriptExecutor)driver).executeScript("window.open()");
 //getting all the handles currently avaialbe
 Set<String> handles=driver.getWindowHandles();
 for(String actual: handles)
 {
  if(!actual.equalsIgnoreCase(currentHandle))
  {
  //switching to the opened tab
  driver.switchTo().window(actual);
  //opening the URL saved.
  driver.get(urlToClick);
  }
 }
 }
}

Kudos! You have successfully performed automation testing with Selenium for switching different browser tabs with the help of Windows Handler method. Now, let us go about it in a different manner.

Up Vote Down Vote 0 Votes
Posted 3 months ago