Why .sendKeys(Keys.chord(Keys.CONTROL, “a”)) does

2019-07-24 14:00发布

I am trying to select text in the text field and delete it. I use chromedriver for linux.

This is my code:

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

But it does not work (actually first line). Why? How to make it work?

Versions: Chrome: Version 28.0.1500.95 ChromeDriver: chromedriver_linux64_2.1/chromedriver_linux64_2.2

4条回答
Animai°情兽
2楼-- · 2019-07-24 14:19

Have you tried to use action builder? For example, from our automation suite:

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

}
查看更多
不美不萌又怎样
3楼-- · 2019-07-24 14:22
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"), "");
}
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-07-24 14:28

It should work:

 webElement = driver.findElement(By.id("txtPassword"));
 webElement.sendKeys(Keys.chord(Keys.CONTROL, "a"), Keys.DELETE);
查看更多
神经病院院长
5楼-- · 2019-07-24 14:35
loginPage.getPasswordField().clear
查看更多
登录 后发表回答