Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
org.openqa.selenium.JavascriptException:SyntaxError:'';通过Selenium使用executeScript时,字符串文字包含未转换的换行符_Javascript_Java_Selenium_Selenium Webdriver_Webdriver - Fatal编程技术网

org.openqa.selenium.JavascriptException:SyntaxError:'';通过Selenium使用executeScript时,字符串文字包含未转换的换行符

org.openqa.selenium.JavascriptException:SyntaxError:'';通过Selenium使用executeScript时,字符串文字包含未转换的换行符,javascript,java,selenium,selenium-webdriver,webdriver,Javascript,Java,Selenium,Selenium Webdriver,Webdriver,org.openqa.selenium.JavascriptException:SyntaxError:“”字符串文字在通过selenium使用executeScript时包含未替换的换行符 executeScript()可以完美地处理单行字符串,例如: String myText = "80120804076"; 但当我试图发送多行字符串时,会引发JavascriptException 代码试用: import org.openqa.selenium.By; import org.open

org.openqa.selenium.JavascriptException:SyntaxError:“”字符串文字在通过selenium使用executeScript时包含未替换的换行符

executeScript()
可以完美地处理单行字符串,例如:

String myText = "80120804076";
但当我试图发送多行字符串时,会引发
JavascriptException

  • 代码试用:

    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class send_large_text {
    
        static WebDriver driver;
        public static void main(String[] args) {
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            driver = new FirefoxDriver();
            driver.get("https://translate.shell.com/");
            //String myText = "80120804076";
            String myText = "No, there is no way to hide the console window of the chromedriver.exe \n"
                + "in the .NET bindings without modifying the bindings source code. This is seen \n"
                + "as a feature of the bindings, as it makes it very easy to see when your code \n"
                + "hasn\'t correctly cleaned up the resources of the ChromeDriver, since the console window \n"
                + "remains open. In the case of some other languages, if your code does not properly clean up \n"
                + "the instance of ChromeDriver by calling the quit() method on the WebDriver object, \n"
                + "you can end up with a zombie chromedriver.exe process running on your machine.";
            (   (JavascriptExecutor) driver).executeScript("arguments[0].value=\'" + myText + "\';", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("textarea.form-control#translateText"))));
        }
    }
    
  • 观察到的错误:

    1544184402064   mozrunner::runner   INFO    Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-foreground" "-no-remote" "-profile" "C:\\Users\\ATECHM~1\\AppData\\Local\\Temp\\rust_mozprofile.b4OAHY7RViE6"
    1544184406841   Marionette  INFO    Listening on port 11919
    1544184406988   Marionette  WARN    TLS certificate errors will be ignored for this session
    Dec 07, 2018 5:36:47 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: W3C
    Exception in thread "main" org.openqa.selenium.JavascriptException: SyntaxError: '' string literal contains an unescaped line break
    Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:13:22.693Z'
    System info: host: 'ATECHM-03', ip: '192.168.1.6', os.name: 'Windows 8', os.arch: 'amd64', os.version: '6.2', java.version: '1.8.0_172'
    Driver info: org.openqa.selenium.firefox.FirefoxDriver
    Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 63.0.3, javascriptEnabled: true, moz:accessibilityChecks: false, moz:geckodriverVersion: 0.23.0, moz:headless: false, moz:processID: 5620, moz:profile: C:\Users\AtechM_03\AppData\..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, platformVersion: 6.2, rotatable: false, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
    Session ID: 31ac155f-8f03-4adf-bc7b-a778f1235351
        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.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
        at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
        at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
        at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
        at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:485)
        at demo.send_large_text.main(send_large_text.java:25)
    
我已经完成了相关的讨论:

  • 爪哇:
  • JavaScript:
参考:


还是不知道我哪里出了问题。有人能帮我吗?

你试过这个方法吗:

String myText = {"No, there is no way to hide the console window of the chromedriver.exe",
                 "in the .NET bindings without modifying the bindings source code. This is seen",
                 "as a feature of the bindings, as it makes it very easy to see when your code",
                 "hasn\'t correctly cleaned up the resources of the ChromeDriver, since the console window",
                 "remains open. In the case of some other languages, if your code does not properly clean up",
                 "the instance of ChromeDriver by calling the quit() method on the WebDriver object,",
                 "you can end up with a zombie chromedriver.exe process running on your machine."}.join("\n");

以下是对我有效的方法:

1) 在每个
\n
之前添加一个额外的斜杠,使其成为
\\n

2) 在撇号
之前添加了额外的斜杠,使其
未\\\\\

"No, there is no way to hide the console window of the chromedriver.exe \\n"
                + "in the .NET bindings without modifying the bindings source code. This is seen \\n"
                + "as a feature of the bindings, as it makes it very easy to see when your code \\n"
                + "hasn\\'t correctly cleaned up the resources of the ChromeDriver, since the console window \\n"
                + "remains open. In the case of some other languages, if your code does not properly clean up \\n"
                + "the instance of ChromeDriver by calling the quit() method on the WebDriver object, \\n"
                + "you can end up with a zombie chromedriver.exe process running on your machine.";

谢谢显示错误,因为类型send_large_text-Syntax error on token“.”的方法join(String)未定义;预期的