简体   繁体   中英

Passing a string value to Javascript Executor in selenium

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM