0

Using Selenium WebDriver, we can type text in textarea using sendKeys like:

driver.findElement(By.xpath("//textarea")).sendKeys("text to type");

but in my case text to type is JSON something like:

{
    "name": "Enter Name",
    "id": "Enter ID",
    "helpText": "Enter Help Text"
}

If I want to type above JSON in textarea, I can store it in string and type it but I need to resolve escape character issues.

In this case, what could be the best solution to type JSON in textarea using JAVA?

1
  • get the text and assign it to String var = text.replaceAll("\"","\\\""); and then send it. Commented Jan 13, 2015 at 11:41

1 Answer 1

1

U can try it out by the comment I gave or you can use JavascriptExecutor to set value in textbox as:

JavascriptExecutor js = (JavascriptExecutor) dw2;
    js.executeScript("document.getElementsByTagname('textarea')[0].value='"
        + <your json string> + "'");
Sign up to request clarification or add additional context in comments.

2 Comments

@TDHM can u please confirm whether it worked? Or is there any other issue u r facing in this? Thanks.
Is the comma order for JS is correct in the snippet?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.