org.openqa.selenium.WebDriverException: Error occurred: Runtime.evaluate encountered an issue: SyntaxError: Token is invalid or unexpected

I'm encountering an issue while trying to execute the following code using Selenium webdriver

The code snippet is:

    WebElement w=driver.findElement(By.xpath("//*[@class='tab']"));

    JavascriptExecutor js=(JavascriptExecutor) driver;

    js.executeScript("arguments[0].setAttribute('disable,'');",w);

The error message reads:

org.openqa.selenium.WebDriverException: unknown error: Runtime.evaluate threw exception: SyntaxError: Invalid or unexpected token

(Session info: chrome=59.0.3071.115)
  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.10240 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 463 milliseconds
Build info: version: '3.3.1', revision: '5234b32', time: '2017-03-10 09:04:52 -0800'
System info: host: 'DESKTOP-AQGDP71', ip: '192.168.2.25', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461591 (62ebf098771772160f391d75e589dc567915b233), userDataDir=C:\Users\SEKAR\AppData\Local\Temp\scoped_dir3836_13558}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=59.0.3071.115, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: 9568bb1918bcb9bfdfbc4afab2cf8294
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:638)
    at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:540)
    at stepdefinition.Cheking.search_with_text_and_check_listed_corectly(Chek`enter code here`ing.java:57)
    at ?

Upon searching with text and verifying listed correctly (Cheking.feature:7)

Answer №1

It seems that you have forgotten to use quotation marks. This could be a simple typo mistake.

js.executeScript("arguments[0].setAttribute('disable','');",w);

Answer №2

Dealing with a similar issue, I found a solution that worked for me:

element.sendKeys("5'7");

If your data contains a single quote, make sure to escape it with a backslash. You can achieve this in Java with the following code snippet:

String text = "5'7";
text = text.replace("'", "\\'");
element.sendKeys(text);

By implementing this change, the data will be sent as "5'7" instead of "5'7".

Answer №3

Murthi pointed out that the main issue is the absence of quotes.

If you are planning to utilize selenium (Python) in conjunction with JavaScript to input text from Python, it is advisable to use json.dumps(python_str)

It's important to note that json dumps automatically adds quotes, so make sure not to enclose the string in quotes when using it in JavaScript

'test.innerText = "{}";'.format(json_dumps(python_str))

JavaScript will interpret it as follows:

'test.innerText = ""hello world"";
, resulting in a sys error:

javascript error: Unexpected identifier hello

If single quotes are used, there will be double quotes within the string

To resolve this issue, avoid adding additional single or double quotes since json.dumps already includes them

"test.innerText = {};".format(json_dumps(python_str))

Additionally, replace all quotes with HTML entity codes

python_str = python_str.replace("'", "'").replace('"', """).replace('`', '`')

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

Is it more efficient to declare a variable or directly return the variable from the Element's getText() promise in Protractor?

I am facing a scenario where I need to retrieve the text of a web element using a promise and then extract a specific part of the string for further processing. Which example, the one at the top or the bottom, should I use? var id = element(by.binding( ...

React Hook causing excessive renders due to custom UseLayoutEffect hook for bounding box calculation

Currently, I am developing a feature to generate a hub and spoke diagram. This involves having a central div with other divs surrounding it, all connected by SVG lines. For a simplified code example, you can check out this code sandbox. To achieve this fu ...

Guidelines for updating values automatically from a database

Currently working on an MVC project and aiming to have the web page update automatically upon loading. For instance, if web page 1 is for adding companies to a database and web page 2 is for deleting them, once web page 2 loads, I want a select box with c ...

What is the reason for selenium not executing before_filters in my application controller?

Currently, I am testing a subdomained Rails 3.0 app with cucumber/capybara/selenium. The subdomain is used to determine the current site in a before_filter in the application controller. All tests are running smoothly, except for those using the @javascri ...

Encountering an issue with undefined ID when utilizing a radio input field in Vue JS

I'm facing an issue and I hope you can assist me with it. My webpage contains Vue scripts and a list of radio selections rendered via Liquid tag and GraphQL results. The problem arises when I click the submit button or select a radio option, resultin ...

Issue with Datepicker not updating when triggered by event handler

Having an issue with my material-UI datepicker where the date is not updating correctly when I try to select a new one. The initial value set in useState works fine, but I want the datepicker to smoothly update when I choose a different date. You can see a ...

Is there a way to adjust a 5-minute countdown interval timer by 1 minute in a react JS application?

I am in need of creating a 5-minute interval timer using react JS, with a 1-minute offset. The current timer I have functions like this: 1:00 => 1:05 => 1:10 => 1:15 => 1:20. However, I require it to be adjusted to display: 1:01 => 1:0 ...

Having trouble executing an npm script - getting an error message that reads "Error: spawn node_modules/webpack/bin/webpack.js EACCES"

After installing and configuring Linux Mint, I encountered an error when trying to run my project with the npm run dev command. The error message "spawn node_modules / webpack / bin / webpack.js EACCES" keeps appearing. I have attempted various methods fo ...

What is the best way to cancel a Promise if it hasn't been resolved yet

Let's consider a situation where I have implemented a search function to make an HTTP call. Each call made can have varying durations, and it is crucial for the system to cancel any previous HTTP requests and only await results from the latest call. ...

Launching a Java jar application with log4j on a Windows Server 2012 R2 operating system

Running a Java jar file with a log4j configuration in a specific location on Windows Server 2012 R2 using PowerShell (or command line) is causing issues. One attempted command is: java -jar C:\myApp\myApp.jar -Duser.dir=C:\myApp -Dlog4j.con ...

Apollo's MockedProvider failing to provide the correct data as expected

I created a function called useDecider that utilizes apollo's useQuery method. Here is the code: useDecider: import { useState } from 'react'; import { useQuery, gql } from '@apollo/client'; export const GET_DECIDER = gql` quer ...

Try implementing the Angular 9 Slice Pipe within your ngFor loop

Struggling to figure out how to properly utilize the Slice pipe from Angular within a for loop. Consider this code snippet: textArray = ['aaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbb', 'cccccccccccccccccccc']; <n ...

Preventing Ng-repeat from refreshing after deleting using $http request

Once I remove an item from my list, the deleted object's data disappears, but the placeholder (empty row) lingers. (I tried using apply() with no luck) I have a standard CRUD interface displaying expenses. <table class="table table-striped"> ...

An assemblage of objects in Java

In my programming project, I defined a class called Car that has a property called location represented by a double array of size 2. My goal was to create an array of Car objects. Below is the code snippet showing what I have done so far: Car[] cars; cars ...

Distinguishing ft.remove() from popBackStack() in Android

I am attempting to eradicate a fragment from my stack. The following code snippet is what I have been employing: FragmentManager fm = getSupportFragmentManager(); if (fm != null) { FragmentTransaction ft = fm.beginTransaction(); Fragme ...

How to align an unordered list vertically in the center using Bootstrap?

Trying to figure out why the ul within this parent div is not centered vertically. Here is a screenshot of the issue: https://i.sstatic.net/ZOc9M.png <div className=" d-flex align-items-center bg-primary "> <ul ...

Can JQuery be used to identify the CSS styles applied to selected elements?

Highlight the text by selecting it and clicking a button. If the text is already highlighted, clicking the button should make the selected text return to normal. Can this be achieved using jQuery and a single button? Is it possible to identify the CSS st ...

Having trouble utilizing the "page down" function efficiently in my web scraper

I have created a small Python script using Selenium to automatically scroll down a webpage. However, my script only scrolls to a certain point because I am unsure of how to set the range parameter to reach the bottom. I currently have it set at 10 just as ...

Is it possible to confirm jQuery AJAX events using Jasmine?

I'm currently working on using Jasmine to write BDD specifications for basic jQuery AJAX requests. I've set up Jasmine in standalone mode through SpecRunner.html and configured it to load jQuery and other necessary .js files. However, I'm fa ...

Exploring data visualization within a JSX Component

I am attempting to dynamically render a Card component that is mapped from an array of objects. However, I am encountering an "unexpected token error" and the URL is not rendering as expected. The goal is to display five cards based on the data within the ...