gpt4 book ai didi

java - 如何使用 Selenium 和 Java 通过 sendKeys 方法传递双引号字符串

转载 作者:太空宇宙 更新时间:2023-11-04 09:24:20 24 4
gpt4 key购买 nike

我正在开发一个 selenium 脚本,其中正在获取 3 个值并将其存储在字符串中。

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

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

我该怎么做?

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

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

最佳答案

尝试这样:

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

关于java - 如何使用 Selenium 和 Java 通过 sendKeys 方法传递双引号字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57917851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com