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
So, here we will write some simple code to do these operations :-
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class RealTabdemo
{
WebDriver driver;
@BeforeTest
public void g() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\Complete selenium\\ChromeDriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.navigate().to("http://www.way2testing.com");
Thread.sleep(3000);
}
@Test
public void h() throws InterruptedException, AWTException
{
//driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL+"t");
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
Robot rbt = new Robot();
rbt.keyPress(KeyEvent.VK_CONTROL);
rbt.keyPress(KeyEvent.VK_T);
rbt.keyRelease(KeyEvent.VK_CONTROL);
rbt.keyRelease(KeyEvent.VK_T);
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
String originalHandle = driver.getWindowHandle();
ArrayList
System.out.println(tabs2.size());
driver.switchTo().window(tabs2.get(1));
driver.get("http://www.onlineifsccodebank.com");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[@id='txtIFSCCode']")).sendKeys("ALLA0211921");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
try
{
driver.findElement(By.xpath(".//*[@id='btnSearch']")).click();
}
catch(Exception e)
{
System.out.println(e.getMessage());
driver.findElement(By.cssSelector("#btnSearch")).click();
}
driver.close();
driver.switchTo().window(tabs2.get(0));
driver.findElement(By.xpath(".//*[@id='post-body-751561208295527719']/div[1]/table/tbody/tr[1]/td[2]/a/img")).click();
Thread.sleep(3000);
driver.switchTo().window(tabs2.get(1));
driver.findElement(By.xpath(".//*[@id='post-body-7737945062927568046']/table[1]/tbody/tr[1]/td[1]/a")).click();
for(String handle : driver.getWindowHandles())
{
if (!handle.equals(originalHandle))
{
driver.switchTo().window(handle);
driver.close();
}
}
driver.switchTo().window(originalHandle);
}
}
Another Example :
Scenarios :
Open the browser & navigate to URL
Open the new Tab by using Robot class
open new URL in new tab
Pass driver control in new tab
Perform some operation in new tab
close the new Tab
pass the driver control back to main parent tab
Here is the sample code, where we will open the browser & hit the URL
Then open a new tab, and open a new Url in new tab
now will perform operation in new tab
close the new tab & come back to parent tab
Another Example :
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Tabtest
{
WebDriver driver;
@BeforeTest
public void h() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\Complete selenium\\ChromeDriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.navigate().to("http://www.way2testing.com");
Thread.sleep(3000);
}
@Test
public void g() throws AWTException, InterruptedException
{
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_T);
Thread.sleep(3000);
String windowhandle = driver.getWindowHandle();
ArrayList tabs = new ArrayList(driver.getWindowHandles());
System.out.println("Total tabs are : " +tabs.size());
driver.switchTo().window((String) tabs.get(1));
driver.navigate().to("http://www.onlineifsccodebank.com");
Thread.sleep(3000);
driver.findElement(By.xpath(".//*[@id='txtIFSCCode']")).sendKeys("UTIB0003109");
driver.findElement(By.xpath(".//*[@id='btnSearch']")).click();
driver.close();
driver.switchTo().window(windowhandle);
Thread.sleep(4000);
}
@Test(priority = 1)
public void I() throws InterruptedException
{
String originalHandle = driver.getWindowHandle();
driver.findElement(By.xpath(".//*[@id='post-body-751561208295527719']/div[1]/table/tbody/tr[1]/td[2]/a/img")).click();
ArrayList tabs2 = new ArrayList(driver.getWindowHandles());
System.out.println("Total tabs are : " +tabs2.size());
driver.switchTo().window((String) tabs2.get(1));
Thread.sleep(4000);
driver.findElement(By.xpath(".//*[@id='post-body-7737945062927568046']/table[1]/tbody/tr[1]/td[1]/a")).click();
Thread.sleep(4000);
for(String handle : driver.getWindowHandles())
{
if (!handle.equals(originalHandle))
{
driver.switchTo().window(handle);
driver.close();
}
}
driver.switchTo().window(originalHandle);
}
}
This blog is more effective and it is very much useful for me.we need more information please keep update more.
ReplyDeleteSelenium Training in Chennai
Selenium Training Institute in Chennai
JAVA Training in Chennai
Python Training in Chennai
Big data training in chennai
IOS Training in Chennai
Selenium Training in Chennai
Selenium Training in OMR