1

I am unable to open google in new tab please review this code :

public static void main (String [] args) throws InterruptedException, AWTException
 {

 System.setProperty("webdriver.chrome.driver","C://Users//vbisht//Downloads//chromedriver.exe");
 WebDriver driver = new ChromeDriver();  
 driver.navigate().to("https://www.google.co.in/");
 Robot r = new Robot();
 r.keyPress(KeyEvent.VK_CONTROL);
 r.keyPress(KeyEvent.VK_T);
 //Thread.sleep(10000);
 //driver.navigate().refresh();*/
 ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
 driver.switchTo().window(tabs.get(1)); //switches to new tab
 driver.navigate().to("https://www.google.co.in/");
Shivam Mishra
  • 1,731
  • 2
  • 11
  • 29
  • Format your code properly when you put a question. – eduPeeth Jul 06 '18 at 11:53
  • Possible duplicate of [How to open a link in new tab (chrome) using Selenium WebDriver?](https://stackoverflow.com/questions/34829329/how-to-open-a-link-in-new-tab-chrome-using-selenium-webdriver) – eduPeeth Jul 06 '18 at 11:57

4 Answers4

0
String parentWindowHandler=driver.getWindowHandle();// Store your parent window
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
  subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler); // switch to popup window

Hope it will help you.

Gautam Bothra
  • 565
  • 1
  • 8
  • 23
0

Try the below code,

The code below will open the link in new Tab.

String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN); 
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);

The code below will open empty new Tab.

String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t");
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);
0

Instead of using Robot Class, You can simply open it with Javascript Executor

public static void main (String [] args) throws InterruptedException, AWTException { System.setProperty("webdriver.chrome.driver","C://Users//vbisht//Downloads//chromedriver.exe"); 
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://www.google.co.in/");
((JavascriptExecutor) driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1)); //switches to new tab
driver.navigate().to("https://www.google.co.in/");
} 
Ishita Shah
  • 3,955
  • 2
  • 27
  • 51
0

To open two different urls in two different TABs, once you initiate opening a new tab/window you have to induce WebDriverWait and then collect the window handles and finally iterate through the window handles and then switchTo().window(newly_opened) as per the following example:

  • Sample Code:

    System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe.exe");
    WebDriver driver =  new InternetExplorerDriver();
    driver.get("http://www.google.com");
    String first_tab = driver.getWindowHandle();
    System.out.println("Working on Google");
    ((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
    WebDriverWait wait = new WebDriverWait(driver,5);
    wait.until(ExpectedConditions.numberOfWindowsToBe(2));
    Set<String> s1 = driver.getWindowHandles();
    Iterator<String> i1 = s1.iterator();
    while(i1.hasNext())
    {
        String next_tab = i1.next();
        if (!first_tab.equalsIgnoreCase(next_tab))
        {
        driver.switchTo().window(next_tab);
    
        System.out.println("Working on Facebook");
        }
    }
    
  • Console Output:

    Working on Google
    Working on Facebook
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352