2

I know this question is already asked.I searched for multiple answers and not found my problem.

if (driver instanceof JavascriptExecutor) {
        System.out.println("In try");
        ((JavascriptExecutor)driver).executeScript("document.getElementById('comment').value='"+line1+line2+"';");
    } else {
        throw new IllegalStateException("This driver does not support JavaScript!");
    }

I am using this code to set the values in text area.line1 and line2 are two string variables.

The result of printing them is

  1. Root Cause Analysis:We have analyzed and found there are duplicate transactions present in our db.We have analyzed the issue and agent it appears to be the problem that feed,That has returned pending transaction as posted due to which transaction got filed for two different dates

We have filed udc 7735 to do the necessary cleanup 2. Fixes implemented to resolve the issue :

i don't think the strings contains any quotation mark but still i am getting

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Runtime.evaluate threw exception: SyntaxError: Unexpected token ILLEGAL
at Object.InjectedScript._evaluateOn (<anonymous>:878:140)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:811:34)
at Object.InjectedScript.evaluate (<anonymous>:667:21)
(Session info: chrome=49.0.2623.87)
(Driver info: chromedriver=2.15.322448   (52179c1b310fec1797c81ea9a20326839860b7d3),platform=Windows NT 6.1 SP1 x86_64)    (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 9 milliseconds
Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:03:33'
System info: host: 'IN-L1115', ip: '192.168.7.126', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_73'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={userDataDir=C:\Users\rkumar11\AppData\Local\Temp\scoped_dir28484_31867}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, version=49.0.2623.87, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: c7e379b5dba4c793ebca9372d241f787
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:577)
at test.main(test.java:211)

I tried using sendKeys method of driver that works but it automatically submits the page or kind of automatic validation is triggered in that page>Therefore i can not use sendKeys function to set the value.

Can somebody please help me how to proceed with it?

Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
Ravi
  • 719
  • 8
  • 23
  • I would try ...executeScript("var str1='"+line1+"';var str2='"+line2+"';document.getElementById('comment').value=str1.concat(str2);"); – MikeJRamsey56 Mar 26 '16 at 01:13
  • i am creating the variables of my own so i concated them whil creating like i made line7=line1+line2+line3.......line5; Now ehen i am using ((JavascriptExecutor)driver).executeScript("document.getElementById('comment').value='"+line7+"';"); still i am getting same exception.Is there something wrong with the syntax? – Ravi Mar 26 '16 at 08:53
  • plz have a look at this http://stackoverflow.com/questions/25583641/set-value-of-input-instead-of-sendkeys-selenium-webdriver-nodejs hope this solves ur problem if not plz let me know – Rajnish Kumar Mar 26 '16 at 14:16
  • It seems to contain a newline character. Try to escaping the string before sending it to JS. – skandigraun Mar 26 '16 at 14:58
  • Yes the strings contains the new line character.How can i skip them inside javascript executor?....I can not replace them as i want to set multiline comments – Ravi Mar 26 '16 at 15:39
  • What about this? var multiStr = [ "This is the first line", "This is the second line", "This is more..." ].join("\n"); – MikeJRamsey56 Mar 27 '16 at 01:57
  • Actually my individual strings are also multiline comments and i can not use multiStr because strings are generated dynamically sometimes they may be of singkle line some times they are multi line depending upon previous comments by user – Ravi Mar 29 '16 at 11:37

1 Answers1

1

Try this:

    if (driver instanceof JavascriptExecutor) {
            System.out.println("In try");
            ((JavascriptExecutor)driver).executeScript("document.getElementById('comment').value=\'" + line1 + line2 + "\';");
    } else {
            throw new IllegalStateException("This driver does not support JavaScript!");
    }

To pass a string element to your script String, You need to put \ before the single quotes. Hope this helps.

Antoine
  • 800
  • 3
  • 14
  • 29