Issue
I want to pass the below value in the script using the javascript executor.
String value = "ac.saveDocket('CompanyRegistration','https://yyy.yyy',0);"
JavascriptExecutor executor = ((driver) as JavascriptExecutor)
WebElement webElement = driver.findElement(By.xpath("//div[@class=\'CodeMirror-code\']/div[1]//pre"))
executor.executeScript("arguments[0].innerHTML='$value';", webElement)
This results in the following error:
org.openqa.selenium.JavascriptException: javascript error: Unexpected identifier
Execution is successful without special characters so the problem is definitely with the single quotes.
How I can pass special characters like single quotes to the JavaScript executor?
Solution
This works on browser console:
arguments[0].innerHTML='ac.saveDocket(\'CompanyRegistration\',\'https://yyy.yyy\',0);'
And for java code use like this:
String value = "ac.saveDocket(\\'CompanyRegistration\\',\\'https://yyy.yyy\\',0);";
WebElement element = driver.findElement(By.xpath("your_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].innerHTML='"+value+"'", element);
Answered By - Norayr Sargsyan
Answer Checked By - Willingham (JavaFixing Volunteer)