如何使用 Selenium 和 Java 通过 sendKeys 方法传递双引号字符串
How to pass double quoted strings through sendKeys method using Selenium and Java
我正在编写一个 selenium 脚本,其中 3 个值被获取并存储在字符串中。
String one = "19292";
String two = "Abc";
String three = "def";
我希望它以(一+二+三)的形式发送文本,但它们都带有双引号。所以最终结果应该是 "19292""Abc""def"
我该怎么做?
我尝试过使用反斜杠来使用转义机制,但是每当我使用它而不是获取字符串值时,它都会打印文本。
例如:
\"one\" prints "one" rather than "19292"
试试这个:
String text = "\"" + one + "\"\"" + two + "\"\"" + three + "\"";
尝试:
String str = "\""+one+"\"\""+two+"\"\""+three+"\"";
System.out.println(str);
这将打印 "19292""Abc""def"
像这样尝试:
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();
}
如果您的用例是将结果字符串作为 "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 you can use the following 的 文本框 发送字符串为 **"19292" "Abc" "def"
** 的示例:
代码块:
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);
}
}
浏览器快照:
我正在编写一个 selenium 脚本,其中 3 个值被获取并存储在字符串中。
String one = "19292";
String two = "Abc";
String three = "def";
我希望它以(一+二+三)的形式发送文本,但它们都带有双引号。所以最终结果应该是 "19292""Abc""def"
我该怎么做?
我尝试过使用反斜杠来使用转义机制,但是每当我使用它而不是获取字符串值时,它都会打印文本。 例如:
\"one\" prints "one" rather than "19292"
试试这个:
String text = "\"" + one + "\"\"" + two + "\"\"" + three + "\"";
尝试:
String str = "\""+one+"\"\""+two+"\"\""+three+"\"";
System.out.println(str);
这将打印 "19292""Abc""def"
像这样尝试:
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();
}
如果您的用例是将结果字符串作为 "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 you can use the following "19292" "Abc" "def"
** 的示例:
代码块:
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); } }
浏览器快照: