Earn Money in 10 days

Selenium Videos

Protractor In Selenium

Livechat

Tuesday, 14 April 2015

Click on a link and switched to different tab/Window.

Switching on tab from a link

When Selenium Webdriver object instantiated then Webdriver assigns an alphanumeric id to each window.This unique alphanumeric id is called window handle.
Each window has a unique ID so that selenium can differentiate when it is switching controls from one window to other.

Set <String> windows =driver.getWindowHandles();
//Set will store the multiple windows

System.out.println(windows.size());
//Print the size of windows

Iterator<String> it = windows.iterator();
//iterate through your windows
while (it.hasNext()){
String parentwindow = it.next();
System.out.println("This is first window id  "+parentwindow);
//This will print the id of first window.
String childwindow = it.next();
System.out.println("this is second window id  "+childwindow);
//This will print the id of second window or Tab

When we are working with selenium sometime we get a challenge like if we click on a link then we switch to a different window or pop up and if we have to enter in the pop up and enter in that page then we need to switch on that pop up.

Here I have given an example of in which we are clicking on a link and switched on different tab and enter in that page.

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MultipleWindows {
public static void main(String str[]) throws InterruptedException{
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.get("http://hdfc.com/customer-login");
//go to this hdfc url
driver.findElement(By.xpath("//a[contains(text(), 'CUSTOMER LOGIN')]")).click();
//Perform the click operation that opens new window
Thread.sleep(5000);
//Wait for 5 sec
Set <String> windows =driver.getWindowHandles();
System.out.println(windows.size());
//Print the size of windows
Iterator<String> it = windows.iterator();
//iterate through your windows
while (it.hasNext()){
String parentwindow = it.next();
System.out.println("This is first window id  "+parentwindow);
String childwindow = it.next();
System.out.println("this is second window id  "+childwindow);
driver.switchTo().window(childwindow);
//Switch to child window
        driver.findElement(By.xpath("//*[@id='tb_usr']")).sendKeys("Seleniumgyan123");
        Thread.sleep(5000);
        driver.close();
//close the child window
Thread.sleep(5000);
driver.switchTo().window(parentwindow);
//switch to parent window
String parenttitle = driver.findElement(By.xpath("//h2[contains(text(), 'ONLINE ACCESS FOR EXISTING CUSTOMERS')]")).getText();
System.out.println(parenttitle);
//print the title of parent window
}
}}

4 comments:

  1. Good Post!!!
    I have just one question, why you have used "Iterator", I think foreach loop is much more easier then Iterator. Also could you let me, how values are stored in WindowHandles() collection, is it in random order or the sequence in which different windows open?

    ReplyDelete
  2. whenever applications and web are beginning to wind up distinctly exceptionally foggy is this the time we can state farewell to an apparatus that empowers us to chop down the measure of hours we have to spend to inspire it to chip away at numerous gadgets? java programming

    ReplyDelete

Popular Posts

 
subscribe
Subscribe Us
emailSubscribe to our mailing list to get the updates to your email inbox... We can't wait more to have your email in our subscribers email list. Just put your nice email in below box: