3

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

这是我的代码:

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

4 回答 4

2

您是否尝试过使用操作生成器?例如,从我们的自动化套件中:

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

}
于 2013-10-21T12:48:53.963 回答
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"), "");
}
于 2014-08-22T15:14:23.647 回答
0
loginPage.getPasswordField().clear
于 2015-12-08T00:17:11.053 回答
0

它应该工作:

 webElement = driver.findElement(By.id("txtPassword"));
 webElement.sendKeys(Keys.chord(Keys.CONTROL, "a"), Keys.DELETE);
于 2018-03-15T03:03:50.853 回答