未知错误:Runtime.evaluate引发异常:SyntaxError:意外的令牌变量

时间:2015-02-11 10:46:10

标签: javascript selenium selenium-webdriver webdriver

我有这段代码:

        JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
//        javascriptExecutor.executeScript("W.setOffer({offer: {kind: \"coupon\", title: \"Hello\", availability: " +
//                "\"available\"}},{})");

        String script =
                "var aLocation = {}" +
                        "var aOffer = {}" +
                        "var aAdData = " +
                        "{ " +
                        "location:  aLocation, " +
                        "offer:    aOffer " +
                        " } " +

                        "var aClientEnv = " +
                        " { " +
                        "    sessionid:     “”, " +
                        "   cookie:        “”, " +
                        "   “rtserver-id”: 1,   " +
                        "       lon:           34.847, " +
                        "       lat:           32.123, " +
                        "       venue:         “”, " +
                        "    venue_context: “”, " +
                        //   AD-{"campaignId":8224,"offerId":4172}
                        // see Venue Context for more information.

                        "    source:        “”," +   // One of the following (string) values: ADS_PIN_INFO,
                        // ADS_0SPEED_INFO, ADS_LINE_SEARCH_INFO,
                        // ADS_ARROW_NEARBY_INFO, ADS_CATEGORY_AUTOCOMPLETE_INFO,
                        // ADS_HISTORY_LIST_INFO
                        // (this field is also called “channel”)

                        "    locale:        “”" + // ISO639-1 language code (2-5 characters), supported formats:
                        // * en/he/fr/…
                        // * en-GB/pt-BR/en_GB/pt_BR
                        // * es-419/es_419
                        " } " +


                        "W.setOffer(aAdData, aClientEnv);";

        javascriptExecutor.executeScript(script);
    }

但它在executeScript

上失败了
org.openqa.selenium.WebDriverException: unknown error: Runtime.evaluate threw exception: SyntaxError: Unexpected token var
(Session info: chrome=42.0.2298.0)
  (Driver info: chromedriver=2.9.248307,platform=Mac OS X 10.9.5 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 18 milliseconds

这是脚本。我错过了什么吗?

我不能使用var来保存元素吗?

var aLocation = {}
var aOffer = {}
var aAdData = {
    location: aLocation,
    offer: aOffer
}
var aClientEnv = {
    sessionid: “”,
    cookie: “”,
    “rtserver - id”: 1,
    lon: 34.847,
    lat: 32.123,
    venue: “”,
    venue_context: “”,
    source: “”,
    locale: “”
}
W.setOffer(aAdData, aClientEnv);

1 个答案:

答案 0 :(得分:3)

问题在于行连接。

 String script =
            "var aLocation = {}" +
                    "var aOffer = {}" +
                    "var aAdData = " +
                    "{ " +
                    "location:  aLocation, " +
                    "offer:    aOffer " +
                    " } "

与:

相同
String script = 
            "var aLocation = {}var aOffer = {}var aAdData = { location:  aLocation, offer:    aOffer  } " 

哪个不是有效的jsavascript。您可以在每个var之间使用新行或;