Hello Friends,
In this article , i will describe that how to handle the Tabs in selenium.
So we will cover below scearios :
For Video part 1:
Click hereFor Video part 2:
Click hereOpen the browser & enter Url Open a new tab
Hit a url in new tab
perform some operation in new tab
come back to main tab
Scenario :2-
Open the browser & enter Url
click some link which will open the new tab
switch to newly opened tab
perform some operation
come back to main tab
Scenario :3-
Open the browser & enter Url click on link which will open the new tab
switch to newly opened tab
close newly opened tab only
perform some operation in your main tab
Scenario :4-
Suppose there is 5 chrome windows open and you need to close all window except parent window
So, here we will write some simple code to do these operations :-
Method 1
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.testng.annotations.Test;
public class practice
{
WebDriver driver;
@Test
public void tabs() throws InterruptedException, AWTException
{ System.setProperty("webdriver.chrome.driver", "./Driver\\chromedriver_99.exe");
System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true");
System.setProperty("webdriver.chrome.silentOutput", "true");
driver = new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
driver.navigate().to("https://google.com");
Thread.sleep(5000);
driver.findElement(By.name("q")).sendKeys("way2testing");
driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
Thread.sleep(2000);
String parentwindow = driver.getWindowHandle();
//Open the New Tab
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_T);
driver.switchTo().window(parentwindow);
Thread.sleep(2000);
ArrayList tabs = new ArrayList(driver.getWindowHandles());
System.out.println(tabs.size());
driver.switchTo().window((String) tabs.get(1));
Thread.sleep(2000);
driver.navigate().to("http://www.way2testing.com");
}
}
Selenium 3
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.testng.annotations.Test;
public class practice
{
WebDriver driver;
@Test
public void tabs() throws InterruptedException, AWTException
{ System.setProperty("webdriver.chrome.driver", "./Driver\\chromedriver_99.exe");
System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true");
System.setProperty("webdriver.chrome.silentOutput", "true");
driver = new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
driver.navigate().to("https://google.com");
Thread.sleep(5000);
driver.findElement(By.name("q")).sendKeys("way2testing");
driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
Thread.sleep(2000);
String parentwindow = driver.getWindowHandle();
//Open the New Tab
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_T);
driver.switchTo().window(parentwindow);
Thread.sleep(2000);
ArrayList tabs = new ArrayList(driver.getWindowHandles());
System.out.println(tabs.size());
driver.switchTo().window((String) tabs.get(1));
Thread.sleep(2000);
driver.navigate().to("http://www.way2testing.com");
}
}
Method 2
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.testng.annotations.Test;
public class practice
{
WebDriver driver;
@Test
public void tabs() throws InterruptedException, AWTException
{
System.setProperty("webdriver.chrome.driver", "./Driver\\chromedriver_99.exe");
System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true");
System.setProperty("webdriver.chrome.silentOutput", "true");
driver = new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
driver.navigate().to("https://google.com");
Thread.sleep(5000);
driver.findElement(By.name("q")).sendKeys("way2testing");
driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
Thread.sleep(2000);
String parentwindow = driver.getWindowHandle();
//Open the New Tab
driver.switchTo().newWindow(WindowType.TAB);
driver.navigate().to("http://www.way2testing.com");
Thread.sleep(3000);
driver.findElement(By.xpath("//*[@id='myTopnav']/a[1]/b")).click();
//Switch Back to previous window
driver.switchTo().window(parentwindow);
}
}
Before Selenium 4, we were using getWindowHandle & getWindowHandles methods.Selenium 4
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.testng.annotations.Test;
public class practice
{
WebDriver driver;
@Test
public void tabs() throws InterruptedException, AWTException
{
System.setProperty("webdriver.chrome.driver", "./Driver\\chromedriver_99.exe");
System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true");
System.setProperty("webdriver.chrome.silentOutput", "true");
driver = new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
driver.navigate().to("https://google.com");
Thread.sleep(5000);
driver.findElement(By.name("q")).sendKeys("way2testing");
driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
Thread.sleep(2000);
String parentwindow = driver.getWindowHandle();
//Open the New Tab
driver.switchTo().newWindow(WindowType.TAB);
driver.navigate().to("http://www.way2testing.com");
Thread.sleep(3000);
driver.findElement(By.xpath("//*[@id='myTopnav']/a[1]/b")).click();
//Switch Back to previous window
driver.switchTo().window(parentwindow);
}
}
driver.getWindowHandle() will return the String so below is syntax :
String currentwindow = driver.getWindowHandle();
Set
List
but in Selenium 4 We can handle the tabs as below :-
WebDriver newTab = driver.switchTo().newWindow(WindowType.TAB)
WebDriver newwndow = driver.switchTo().newWindow(WindowType.WINDOW);
window handling in selenium
Question : Suppose there is 5 chrome window opened and except parent window, need to close all window.Answer : use driver.getWindowHandle(), this will return parent window
driver.getWindowHandles() returns a set of all open window handles.
driver.switchTo().window(windowHandle) is used to switch the driver’s context to a specific window.
driver.close() closes the current window.
Handle window
public class windowDemo{
public static void main(String[] args){
WebDriver driver = new ChromeDriver();
String parentwindow = driver.getWindowHandle();
// ArrayList<String> allwindow = new ArrayList<>(driver.getWindowHandles());
Set<String> totalwindow = driver.getWindowHandles();
for(String win : totalwindow){
if(!parentwindow.equals(win)){
driver.switchTo().window(win);
driver.close();
}
}
}
}
in selenium using java
public class windowDemo{
public static void main(String[] args){
WebDriver driver = new ChromeDriver();
String parentwindow = driver.getWindowHandle();
// ArrayList<String> allwindow = new ArrayList<>(driver.getWindowHandles());
Set<String> totalwindow = driver.getWindowHandles();
for(String win : totalwindow){
if(!parentwindow.equals(win)){
driver.switchTo().window(win);
driver.close();
}
}
}
}
No comments:
Post a Comment