I've been facing an issue while trying to run Javascript within python's selenium Chromedriver. Despite researching the error mentioned in this link unknown error: Runtime.evaluate threw exception: SyntaxError: Unexpected token var and attempting to properly format the js as suggested, I couldn't resolve the problem.
The script I'm using is:
$.ajax({
type: "POST",
data: {
"code": "a=input(\"\");\nprintf(\"Part A\\n\");\nSA=6*a^2;\nprintf(\"SA = %d\\n\",SA);\nr1=(6*a^2/(4*pi))^(1/2);\nprintf(\"Radius = %.2f\\n\",r1);\nprintf(\"Part B\\n\");\nv=a^3;\nr2=(3*v/(4*pi))^(1/3);\nprintf(\"Radius = %.2f\\n\",r2);\n",
"input": "",
},
url: "code.evaluate.abc.php",
dataType: "json",
});
As it's not possible to input this code directly into driver.execute_script(javascript_string) due to newlines, I had to remove them to condense it into one line:
$.ajax({type: "POST",data: {"code": "a=input(\"\");\nprintf(\"Part A\\n\");\nSA=6*a^2;\nprintf(\"SA = %d\\n\",SA);\nr1=(6*a^2/(4*pi))^(1/2);\nprintf(\"Radius = %.2f\\n\",r1);\nprintf(\"Part B\\n\");\nv=a^3;\nr2=(3*v/(4*pi))^(1/3);\nprintf(\"Radius = %.2f\\n\",r2);\n","input": "",},url: "code.evaluate.abc.php",dataType: "json"});
This condensed version functions correctly when pasted into the browser's developer console, but running it from inside driver.execute_script results in the following error:
WebDriverException: unknown error: Runtime.evaluate threw exception: SyntaxError: Invalid or unexpected token (Session info: chrome=65.0.3325.162) (Driver info: chromedriver=2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881),platform=Linux 4.13.0-37-generic x86_64)
P.S.: Console.log, alerts, small javascript commands, and other webdriver commands execute without any issues.