Received org.openqa.selenium.JavascriptException: SyntaxError: The string literal contains an unescaped line break while utilizing executeScript in Selenium.
executeScript()
works flawlessly with a single-line String, like this example:
String myText = "80120804076";
However, when attempting to send a multiline String, it results in a JavascriptException
.
Here are the code trials:
import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class send_large_text { static WebDriver driver; public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); driver = new FirefoxDriver(); driver.get("https://translate.shell.com/"); String myText = "No, there is no way to hide the console window of the chromedriver.exe \n" + "in the .NET bindings without modifying the bindings source code. This is seen \n" + "as a feature of the bindings, as it makes it very easy to see when your code \n" + "hasn\'t correctly cleaned up the resources of the ChromeDriver, since the console window \n" + "remains open. In the case of some other languages, if your code does not properly clean up \n" + "the instance of ChromeDriver by calling the quit() method on the WebDriver object, \n" + "you can end up with a zombie chromedriver.exe process running on your machine."; ( (JavascriptExecutor) driver).executeScript("arguments[0].value=\'" + myText + "\';", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("textarea.form-control#translateText")))); } }
Error Encountered:
1544184402064 mozrunner::runner INFO Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-foreground" "-no-remote" "-profile" "C:\\Users\\ATECHM~1\\AppData\\Local\\Temp\\rust_mozprofile.b4OAHY7RViE6" // More error logs here
I've reviewed the related discussions:
- Java:
- Java multiline string
- JavaScript:
- How do I break a string across more than one line of code in JavaScript?
For further reference: SyntaxError: unterminated string literal
If anyone has insights on where I might be going wrong, please assist. Thank you!