首页 > 解决方案 > 使用 Selenium Webdriver 发送多个空格/空格似乎是不可能的

问题描述

你能告诉我如何使用 sendkeys 或类似的东西发送多个空格/空格。我正在尝试测试是否接受在其他字符之间使用多个空格来创建用户名(比如说“用户名”)。我没有成功使用字符串、使用 sendkeys“用户名”或多次使用 sendkeys(Keys.SPACE)。它总是减少到一个空格。非常感谢。

标签: selenium-webdriverwhitespacesendkeys

解决方案


尝试sendKeys()使用 Actions 的方法发送它。

public void sendTextAction(By locator, String text){
    Actions action = new Actions(driver);
    clickVisible(locator);
    action.sendKeys(text).build().perform();
}

如果这仍然不起作用,请尝试多次使用此方法,即发送“用户名”试试这个:

sendTextAction(elementLocator,"user");
sendTextAction(elementLocator," ");
sendTextAction(elementLocator," ");
sendTextAction(elementLocator," ");
sendTextAction(elementLocator,"name");

但首先试试这个

sendTextAction(elementLocator,"user   name");

推荐阅读