我正在尝试选择文本字段中的文本并将其删除。 我在 Linux 上使用 chromedriver。
这是我的代码:
loginPage.getPasswordField().sendKeys(Keys.chord(Keys.CONTROL, "a"));
loginPage.getPasswordField().sendKeys(Keys.DELETE);
但是它不起作用(实际上是第一行)。为什么?如何让它发挥作用?
版本: Chrome:版本 28.0.1500.95 Chrome驱动程序:chromedriver_linux64_2.1/chromedriver_linux64_2.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();
}
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"), "");
}
它应该有效:
webElement = driver.findElement(By.id("txtPassword"));
webElement.sendKeys(Keys.chord(Keys.CONTROL, "a"), Keys.DELETE);
loginPage.getPasswordField().clear
添加“+”图标而不是“,”即可工作
loginPage.getPasswordField().sendKeys(Keys.chord(Keys.CONTROL + "a")); loginPage.getPasswordField().sendKeys(Keys.DELETE);