无法使用MAC上的JAVA在Selenium Webdriver中创建或切换到新选项卡

无法使用MAC上的JAVA在Selenium Webdriver中创建或切换到新选项卡,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我尝试了一个不同的选项来打开新的标签,但所有的选项都会导致相同的结果:将char“t”发送到google搜索字段。 我真正测试的目标是在浏览器中的选项卡之间切换,但我甚至无法打开新的选项卡 非常简单的测试 public class LoginPhp2 { @Test public void testGoogle() { WebDriver driver = new SafariDriver(); drive

我尝试了一个不同的选项来打开新的标签,但所有的选项都会导致相同的结果:将char“t”发送到google搜索字段。 我真正测试的目标是在浏览器中的选项卡之间切换,但我甚至无法打开新的选项卡

非常简单的测试

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


        }
    }

您可以使用javascript打开一个新选项卡

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

在windows和Mac中打开新选项卡的方式多种多样。要发送的实际密钥取决于您的操作系统,例如,Mac使用COMMAND+t,而不是CONTROL+t

您可以使用以下命令在Mac中打开新选项卡:

driver.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND +"t");
之后,您可以切换到任何打开的选项卡:

ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(0));
ArrayList tabs=新建ArrayList(driver.getWindowHandles());
driver.switchTo().window(tabs.get(0));

我遵循了前面建议的方法:

JavascriptExecutor js=(JavascriptExecutor)驱动程序
js.executeScript(“window.open();”)


这是在同一窗口中打开新选项卡。我已经在我的MAC电脑上试用过了

,所以第一个有焦点的dom元素是搜索输入?首先尝试模糊它?尝试使用javascriptexecutor。我相信这将打开新窗口,但不会在同一窗口中打开新选项卡这将在同一窗口中打开新选项卡,直到没有帮助为止;(希望这会有所帮助,您可以尝试取消阻止弹出窗口,然后使用javascript执行window.open