Error: Runtime evaluation encountered an issue due to SyntaxError with an unexpected token 'var'

Here is the code I am working with:

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

The code fails at the executeScript phase.

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

I have double-checked the script. Is there something crucial that I might be overlooking?

Is it not permissible to use the var keyword to store 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);

Answer №1

The issue lies in the concatenation of lines.

Here is an example:

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

This results in :

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

This is not valid JavaScript. To resolve this, you should use new lines or ; between each variable declaration.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Generate a backup link for when the app is not installed by utilizing JavaScript

I am in the process of developing a plugin that will transform regular links into deep links for users on Android or iOS devices. For example, it will identify the link and replace it with a deep link like this: var facebookURL = $('[href*="facebook ...

Guidelines for allocating memory for a dense array in Javascript

When utilizing the new Array(size) constructor, it appears that if the value of size is not constant, certain browsers (such as Chrome) may create a sparse array, leading to slower access times compared to using the default constructor. You can see this pe ...

Modifications made in ajax success do not appear on the page without a manual refresh

I have a JavaScript AJAX block that performs some operations and then updates a text box on my webpage. $.ajax({ type: "POST", url: "/update_page/", data: JSON.stringify({'itemId': parseInt(item_id)}), cache: false, success: ...

Steps to use Hot Keys to exit the browser in Selenium

Selenium Webdriver v-3.0.1 I am attempting to close the browser window using different hotkey combinations, but none of them seem to be working. Here are the methods I have tried so far: driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.CON ...

What are the differences between performing a search in MongoDB (database component) and Node.js (server-side)?

1) My input consists of a string with approximately 30 comma-separated elements, such as str1, str2, str3, etc. This is represented by "INPUT_STRING". 2) Within my MongoDB collection, I have a set of "allowed_strings" which includes entries like str1, str ...

Jenkins process encounters issues with sed execution

I am currently facing an issue where a script that runs successfully locally encounters difficulties during our Jenkins build process, specifically with the 'sed' command. Below is the code snippet I am using. I have double-checked the file path ...

Cannot update VUEjs array following the splice operation

My array is only updating in the console and not in the DOM. I've already tried using :key but it's still not working. Here is my code: <div style="margin-top: 5px;" v-for="(file, index) in editedItem.file_name" ...

Error: Property 'config' cannot be accessed because it is null

Upon transferring my Angular project from my local computer to a Linux server, I attempted to launch the project using ng serve but encountered an issue where it opened a new file in the console editor instead. After some investigation, I tried running np ...

What is the best method for effectively organizing and storing DOM elements alongside their related objects?

In order to efficiently handle key input events for multiple textareas on the page, I have decided to create a TextareaState object to cache information related to each textarea. This includes data such as whether changes have been made and the previous co ...

"Effortlessly attach and send multiple images via email using Dropzone in

I am encountering an issue with uploading multiple image files and sending them via email to customers. The ajax request is throwing an error call to a member function getclientoriginalname() on array when the uploadMultiple: true, option is added to dropz ...

What is the best way to modify a state in nextjs?

Recently, I've been working on a next.js project that includes a shopping cart feature. Essentially, when you click on an item, its image is added to a list and displayed with all the other selected items. To remove an item, users can simply click on ...

The problem of a static click function not working when clicked on a link. What is the solution for this

Details I am currently using a flickity slideshow that automatically goes to the next picture on a click. Within the slideshow, I have placed a button with text and a link to an external website (e.g. ). My Problem: When I click on the link, my slidesho ...

Unable to locate Ckeditor toolbar option within Vue (Laravel) framework

Currently, I am utilizing Ckeditor5 for Vue in Laravel. In accordance with the provided documentation, I have gone ahead and installed the necessary modules using the command npm install --save @ckeditor/ckeditor5-vue @ckeditor/ckeditor5-build-classic. Fol ...

Unclear outcomes from iterative loops

I have a question about this particular for loop: for (var i=0,j=0;i<4,j<20;i++,j++) { a=i+j; } console.log(a); Can someone explain why the output is 38? I initially expected it to be 6. ...

Does Python BrowserMob Proxy capture inaccurate HAR files when using Internet Explorer?

I am currently experimenting with BrowserMob Proxy (v2.1.1) in conjunction with Selenium (v2.5.3) for Python (v2.6) to analyze page load times and save them as HAR files. My goal is to test the performance on both Chrome and IE browsers. Although I have su ...

Using Three.js to seamlessly transition from one Panorama Cube to another with a smooth zoom-in effect

As a newcomer to Three.js, I am currently experimenting with an example featuring a set of 6 image cubes for a panorama effect. This allows users to pan, zoom in, and zoom out around the cubes. If you're interested, you can check out the example here ...

Is it possible to bypass the same-origin policy when creating AJAX client code for a Google AppEngine site?

I am collaborating with someone who has a Google AppEngine website with a custom API located there. My goal is to create AJAX Javascript code that interacts with the custom API on the site, but I am facing difficulties due to the same-origin policy. JSONP ...

Is there a way to access the contents of ng-repeat using getElementById?

Is there a way to extract the data from ng-repeat? <div ng-repeat="w in quick.wint"> <div id="wname"><strong>{{w.Name}}</strong></div> <div id="wcount">{{w.Count}} Open Queues</div& ...

Utilize texture in shader material incorporated into object loaded with object loader in three.js

I have encountered an issue while trying to incorporate a texture into a shader material used in a Blender object loaded through the object loader THREE.OBJLoader. The following simple code functions correctly, loading a texture and applying it to the loa ...

Troubleshooting issues with Vue.js page routing

Currently, I am diving into the world of vue.js and encountering difficulties with setting up routing. My goal is to utilize templates stored in separate HTML files, rather than inline templates. The issue I'm facing is that the routing does not seem ...