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();


        }
    }
2
  • So the first dom element with focus is the search input? Try blurring it first? Commented Jun 5, 2017 at 0:03
  • Try using javascriptexecutor. troubleshootblog.com/2014/11/06/… Commented Jun 5, 2017 at 0:33

3 Answers 3

1

You may use javascript to open a new tab.

JavascriptExecutor js = (JavascriptExecutor) driver;  
js.executeScript("window.open();");
Sign up to request clarification or add additional context in comments.

5 Comments

I believe this will open new window but not new tab in the same window
This will open new tab in the same window
Still did not helped ;(
and you may try to unblock popups and then do window.open using javascript
1

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

Comments

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.