为什么.sendKeys(Keys.chord(Keys.CONTROL,“a”))在Chrome中不起作用

时间:2013-08-19 17:23:51

标签: selenium webdriver selenium-chromedriver

我正在尝试在文本字段中选择文本并将其删除。 我使用chromedriver for linux。

这是我的代码:

loginPage.getPasswordField().sendKeys(Keys.chord(Keys.CONTROL, "a"));
loginPage.getPasswordField().sendKeys(Keys.DELETE);

但它不起作用(实际上是第一行)。为什么?如何使它工作?

版本: Chrome:版本28.0.1500.95 ChromeDriver:chromedriver_linux64_2.1 / chromedriver_linux64_2.2

4 个答案:

答案 0 :(得分:1)

您是否尝试过使用动作构建器? 例如,来自我们的自动化套件:

public void selectAndDeleteTextViaKeyboard() {
    selectTextViaKeyboard()
    deleteViaKeyboard() 
}

public void deleteViaKeyboard() {
    Actions builder = new Actions(webDriverProxy.getWebDriver());
    builder.sendKeys(Keys.DELETE)
            .release().perform();
}

public void selectTextViaKeyboard() {
    Actions builder = new Actions(webDriverProxy.getWebDriver());
    Action select= builder
            .keyDown(Keys.CONTROL)
            .sendKeys("a")
            .keyUp(Keys.CONTROL)
            .build();
    select.perform();

}

答案 1 :(得分:1)

public void copyToClipbord(String copyTo)
{
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection str = new StringSelection(copyTo);
    clipboard.setContents(str, null );
}

public void setText(WebElement element, String value)
{
    copyToClipbord(value);
    element.click();
    element.sendKeys(Keys.chord(Keys.CONTROL, "v"), "");
}

答案 2 :(得分:1)

loginPage.getPasswordField().clear

答案 3 :(得分:0)

应该有效:

 webElement = driver.findElement(By.id("txtPassword"));
 webElement.sendKeys(Keys.chord(Keys.CONTROL, "a"), Keys.DELETE);