Madhusudhan R
Madhusudhan R

Reputation: 321

How to open multiple tab in the same browser?

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

public class newtab {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String driverpath = "geckodriver path\\";
        System.setProperty("webdriver.gecko.driver",driverpath+"geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.google.com");
        driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
        driver.get("http://www.gmail.com"); 


    }

}

In my code I want to open two tabs in the same browser, but this code is opening only one tab. How do I open multiple tabs in the same browser?

Upvotes: 2

Views: 8507

Answers (3)

aadityacool
aadityacool

Reputation: 1

I have written simple code and it worked for me. It is opening two different URLs in different tabs of same browser.

public void cls(){
    WebDriver driver = new FirefoxDriver();

driver.get("http://google.com");
String baseTab = driver.getWindowHandle();
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
driver.get("http://gmail.com"); 

} 

Upvotes: 0

Khushboo Chaudhary
Khushboo Chaudhary

Reputation: 95

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.Set;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class NewTab {

    public static void main(String[] args) throws AWTException {
        System.setProperty("webdriver.gecko.driver", "C:\\Users\\User-024\\Downloads\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.google.com");
        String parent = driver.getWindowHandle();

        Robot r = new Robot();
        r.keyPress(KeyEvent.VK_CONTROL);
        r.keyPress(KeyEvent.VK_T);

        Set<String> browsers = driver.getWindowHandles();
        for (String i : browsers) {
            if (!i.equals(parent)) {
                driver.switchTo().window(i);
                driver.get("http://www.gmail.com");
            }
        }
    }
}

This code is working for me.

Upvotes: 2

Kushal Bhalaik
Kushal Bhalaik

Reputation: 3384

You are not able to open Gmail in other tab because focus is still at 1st window, because selenium identifies a particular window to work with using its window handles hence you have to first switch that particular window using handle like: driver.switchTo().window(handle value)

here is the complete code:

 public static void main(String[] args) {

    System.setProperty("webdriver.chrome.driver", "c:\\SRP\\chromedriver.exe");

    WebDriver driver = new ChromeDriver();
    driver.get("https://www.google.com");

    String newTab =null;
    String baseTab = driver.getWindowHandle();

    driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");

    Set <String> allTabs = driver.getWindowHandles();

    allTabs.remove(baseTab);

    Iterator<String> itr = allTabs.iterator();

    while(itr.hasNext()){


    newTab = (String) itr.next();

    }

    driver.switchTo().window(newTab);
    driver.get("http://www.gmail.com"); 

}

Upvotes: 2

Related Questions