1

I am working on a selenium script where 3 values are being fetched and store in String.

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

I want it to send text as (one + two + three) but all of them is having double quotes. So end result should be "19292""Abc""def"

How can I do this ?

I have tried using the escape mechanism using back slash, but whenever I use it rather than fetching the string value it prints the text. For Eg :

\"one\" prints "one" rather than "19292"
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Possible duplicate of [How to enter quotes in a Java string?](https://stackoverflow.com/questions/3559063/how-to-enter-quotes-in-a-java-string) – Dinesh K Sep 13 '19 at 06:41

4 Answers4

0

Try this:

String text = "\"" + one + "\"\"" + two + "\"\"" + three + "\"";
Dietrich
  • 681
  • 5
  • 18
0

Try:

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

This will print "19292""Abc""def"

Divya Jha
  • 21
  • 5
0

Try sth like this:

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

I just checked with selenium and it works. The input which goes to field is : "19292""Abc""def"

Or if you don't know the number of Strings then below is the method which will convert in quotes format.

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();
}
Rafał Sokalski
  • 1,817
  • 2
  • 17
  • 29
0

If your usecase is to pass the resultant string as "19292" "Abc" "def", then definitely the following Strings declarations won't help the purpose:

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

Instead you need to do the following:

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

An example to send the strings as **"19292" "Abc" "def"**with the textbox of Google Home Page you can use the following Locator Strategies:

  • Code Block:

    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);
        }
    }
    
  • Browser Snapshot:

quoted_string_sendKeys

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352