简体   繁体   English

将字符串值传递给Selenium中的Javascript Executor

[英]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. 我正在使用此代码设置文本区域中的值.line1和line2是两个字符串变量。

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 根本原因分析:我们已经分析并发现数据库中存在重复的事务。我们已经分析了问题和代理商,这似乎是feed的问题,由于已提交了两个不同的事务,该事务已返回已发布的待处理事务日期

We have filed udc 7735 to do the necessary cleanup 2. Fixes implemented to resolve the issue : 我们已提交udc 7735进行必要的清理2。为解决此问题而实施的修复:

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. 我尝试使用sendKeys的驱动程序的sendKeys方法,但该方法会自动提交页面或在该页面中触发某种自动验证>因此,我无法使用sendKeys函数来设置值。

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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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