如何使用Selenium WebDriver alt单击链接

如何使用Selenium WebDriver alt单击链接,selenium,selenium-webdriver,Selenium,Selenium Webdriver,我想使用相同的WebDriver实例在新窗口中打开一个链接。这是到目前为止我的代码 import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class FirstTest { public void driverIsTh

我想使用相同的WebDriver实例在新窗口中打开一个链接。这是到目前为止我的代码

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

public class FirstTest    {

    public void driverIsTheKing()    {

        WebDriver driver = new FirefoxDriver();

        driver.get("http://www.google.com");

        driver.findElement(By.linkText("Gmail")).sendKeys(Keys.ALT,Keys.ENTER);
    }
}

这是行不通的。我需要在按住alt或option键的同时模拟单击链接。我正在OS X上运行此脚本。

在Selenium Webdriver中使用Actions Builder。下面是一个例子:

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement link = driver.findElement(By.linkText("Gmail"));
Actions builder = new Actions(driver);
Action altClick = builder
          .keyDown(Keys.ALT)
          .click(link)
          .keyUp(Keys.ALT)
          .build();

altClick.perform();

你接近答案了。将Keys.ALT替换为Keys.CONTROL

尝试过这个,但在Selenium 2.4.0、Firefox 28、OS X上不起作用。是否适合您?您是否尝试过最新的webdriver绑定,即2.41.0?是的,我刚刚尝试过,但仍然没有成功。光标从鼠标指针更改为手指针,但新窗口不会打开。运行良好吗?实际答案是使用Keys.SHIFT,但感谢您提醒我尝试其他组合。解决方案是使用Keys.SHIFT而不是Keys.ALTAlso。请注意,此代码适用于Selenium Server 2.41.0,但不适用于2.40.0
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FirstTest {

    public void driverIsTheKing() {

    WebDriver driver = new FirefoxDriver();

    driver.get("http://www.google.com");

    driver.findElement(By.linkText("Gmail")).sendKeys(Keys.SHIFT,Keys.ENTER);
    }
}