Tuesday 27 January 2015

Handling Multiple Tabs in Selenium Webdriver

Hi, In this post I'm gonna explain you how to handle multiple tabs in selenium webdriver, well it is quite easy to handle it but remember selenium webdriver will open new window instead of new tab.

For Handling multiple tabs I'm going to use URL:http://irctc.co.in/, kindly execute below mentioned code.

Code:

package NewTabtest;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.ArrayList;

public class NewTab {

    public static void main(String[] args) {
        try{
           
            WebDriver driver = new FirefoxDriver();
            driver.get("https://www.irctc.co.in");
           
            driver.manage().window().maximize();
           
            driver.findElement(By.xpath(".//*[@id='bluemenu']/ul/li[4]/a")).click();
           
            ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
    

          //Switching to new tab 'Tourist Trains'.
            driver.switchTo().window(tabs2.get(1));
           
            String me = "Tourist Trains";
           
            String mee = driver.findElement(By.xpath(".//*[@id='mainpannel']/div/h1")).getText();
           
            if(mee.equals(me))
            {
                System.out.println("Matches");
            }
           
            else
            {
                System.out.println("Doesn't matches");
            }

           //Closing the second tab 'Tourist Trains'
            driver.close();

          //Switching back to Home Tab
            driver.switchTo().window(tabs2.get(0));
           
            String se = "View All";
           
            String see = driver.findElement(By.xpath("html/body/div[1]/div/div[10]/div/div/div[1]/span/a")).getText();
           
           
            if(see.equals(se))
            {
                System.out.println("Matches");
            }
           
            else
            {
                System.out.println("Doesn't matches");
            }
           
            driver.close();
        }
   
    catch (Exception e)
    {
    }
    }
}


So this is how we handle multiple tabs, i hope you guys understood this post..:)

1 comment:

  1. The biggest change in Selenium recently has been the inclusion of the WebDriver API. Driving a browser natively as a user would either locally or on a remote machine using the Selenium Server it marks a leap forward in terms of browser automation and some

    free selenium tutorials.

    ReplyDelete