1

I've tried a different options just to open new tab but all of them lead to the same result : sending char "t" to google search field. The goal in my real test is to switch between tabs in browser, but I am unable even open new one.

Very simple test

    public class LoginPhp2 {

        @Test
        public void testGoogle() {
            WebDriver driver = new SafariDriver();
            driver.get("https://www.google.com");
            //driver.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND + "t");
            Actions action= new Actions(driver);
           action.keyDown(Keys.COMMAND).sendKeys("t").build().perform();
            //action.keyDown(Keys.COMMAND).sendKeys("t").keyUp(Keys.COMMAND).build().perform();


        }
    }
  • So the first dom element with focus is the search input? Try blurring it first? – Tim Jun 05 '17 at 00:03
  • Try using javascriptexecutor. https://troubleshootblog.com/2014/11/06/selenium-opening-a-new-tab-in-browser-firefox-chrome-or-safari/ – coolswastik Jun 05 '17 at 00:33

3 Answers3

1

You may use javascript to open a new tab.

JavascriptExecutor js = (JavascriptExecutor) driver;  
js.executeScript("window.open();");
frianH
  • 7,295
  • 6
  • 20
  • 45
0

There are different ways of opening new tabs in windows and Mac. Actual keys to send depend on your OS, for example, Mac uses COMMAND + t, instead of CONTROL + t.

You can open new tab in Mac using:

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

After this you can switch to any of the opened tabs:

ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(0));
Pang
  • 9,564
  • 146
  • 81
  • 122
Monika
  • 714
  • 1
  • 4
  • 10
0

I have followed the approach suggested earlier:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.open();");

This is opening new tab in the same window. I have tried it on my MAC machine

Monika
  • 714
  • 1
  • 4
  • 10