最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - nknown error: Runtime.evaluate threw exception: SyntaxError: Unexpected token var - Stack Overflow

programmeradmin1浏览0评论

I have this code:

        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);
    }

but it fails on the 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

here is the script. Am I missing something?

cannot I use var to save an element?

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);

I have this code:

        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);
    }

but it fails on the 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

here is the script. Am I missing something?

cannot I use var to save an element?

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);
Share Improve this question asked Feb 11, 2015 at 10:46 Elad BendaElad Benda 36.7k92 gold badges284 silver badges493 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

The problem is line concatenation.

This

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

is the same as :

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

Which isn't valid jsavascript. you can either use new lines or ; between each var

发布评论

评论列表(0)

  1. 暂无评论