Why .sendKeys(Keys.chord(Keys.CONTROL, “a”)) does not work in Chrome

江枫思渺然 提交于 2019-12-24 13:24:50

问题


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


回答1:


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

}



回答2:


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"), "");
}



回答3:


loginPage.getPasswordField().clear



回答4:


It should work:

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


来源:https://stackoverflow.com/questions/18319547/why-sendkeyskeys-chordkeys-control-a-does-not-work-in-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!