【问题标题】:How to pass double quoted strings through sendKeys method using Selenium and Java如何使用 Selenium 和 Java 通过 sendKeys 方法传递双引号字符串
【发布时间】:2019-09-14 03:07:47
【问题描述】:

我正在编写一个 selenium 脚本,其中 3 个值被获取并存储在 String 中。

String one = "19292";
String two = "Abc";
String three = "def";

我希望它以(一+二+三)的形式发送文本,但它们都带有双引号。所以最终结果应该是"19292""Abc""def"

我该怎么做?

我尝试过使用反斜杠的转义机制,但每当我使用它而不是获取字符串值时,它都会打印文本。 例如:

\"one\" prints "one" rather than "19292"

【问题讨论】:

标签: java selenium selenium-webdriver webdriver double-quotes


【解决方案1】:

试试这样:

field.sendKeys("\"" + one + "\"\"" + two + "\"\"" + three + "\"");

我刚刚检查了 selenium,它可以工作。进入字段的输入是: "19292""Abc""def"

或者如果你不知道字符串的数量,那么下面是转换成引号格式的方法。

public static void main(String[] args) {
    String one = "19292";
    String two = "Abc";
    String three = "def";

    System.out.println(surroundWithDoubleQuotes(one, two, three));


}

public static String surroundWithDoubleQuotes(String... input) {
    if(input == null || input.length == 0) {
        return "";
    }
    StringBuilder builder = new StringBuilder();
    for(String arg : input) {
        builder.append("\"\"");
        builder.append(arg);
    }
    builder.append("\"");
    builder.deleteCharAt(0);

    return builder.toString();
}

【讨论】:

  • field.sendKeys("\"" + 一 + "\"\"" + 二 + "\"\"" + 三 + "\"");这行得通。非常感谢
【解决方案2】:

试试这个:

String text = "\"" + one + "\"\"" + two + "\"\"" + three + "\"";

【讨论】:

  • 我无法编辑字符串值。正在从文件中获取它。
【解决方案3】:

试试:

String str = "\""+one+"\"\""+two+"\"\""+three+"\"";
System.out.println(str);

这将打印"19292""Abc""def"

【讨论】:

    【解决方案4】:

    如果您的用例是将结果字符串作为 "19292" "Abc" "def" 传递,那么以下 Strings 声明肯定无济于事:

    String one = "19292";
    String two = "Abc";
    String three = "def";
    

    相反,您需要执行以下操作:

    String one = " \"19292\" ";
    String two = " \"Abc\" ";
    String three = " \"def\" ";
    

    使用Google Home Page文本框 发送字符串为**"19292" "Abc" "def"** 的示例,您可以使用以下Locator Strategies

    • 代码块:

      public class A_demo 
      {
          public static void main(String[] args) throws Exception 
          {
              String one = " \"19292\" ";
              String two = " \"Abc\" ";
              String three = " \"def\" ";
              System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
              ChromeOptions options = new ChromeOptions();
              options.addArguments("start-maximized");
              options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
              options.setExperimentalOption("useAutomationExtension", false);
              WebDriver driver = new ChromeDriver(options);
              driver.get("https://www.google.com/");
              new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.name("q"))).sendKeys(one + two + three);
          }
      }
      
    • 浏览器快照:

    【讨论】:

    • 我没有声明字符串。它们是从文件中获取的。所以我不能编辑它们
    猜你喜欢
    • 1970-01-01
    • 2010-11-19
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多