Tips for displaying javascript code containing variables in the executeScript method

I'm currently working on a selenium script that involves executing JavaScript code using the executeScript method. I've run into an issue when it comes to handling single (') and double quotes (") while passing variables.

Not Working:

js.executeScript("return document.querySelector('input[value=\" '"+group_name+"' \"]').value;").toString();

Working with static value:

js.executeScript("return document.querySelector('input[value=\" DemoTest \"]').value;").toString();

In the line

('input[value=\" '"+group_name+"' \"]')
, the group_name variable is enclosed by Java variable quotes.

Essentially, there are three quotes required for this passage:
1. Before the return statement " "
2. Within querySelector ''
3. When passing the variable value to querySelector ""

Please Note: The DOM only accepts these if they're passed through different quotes with inner strings 2 and 3.

However, I'm encountering a SyntaxError from the DOM that states

SyntaxError : missing ) after argument list

So how can I pass the variable in a way that the DOM will accept it with the required syntax?

Answer №1

Check out this code snippet:

js.executeScript("return document.querySelector('input[value=\"' + arguments[0] + '\"]').value;", group_name).toString();

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 possible for an object to receive notifications when a component object undergoes changes in Angular 2/4?

When working with Angular components, it's possible to pass a variable or object as @Input(). The component will be notified whenever the value of this input changes, which is pretty cool... I'm currently developing an application that features ...

Migrating from jQuery to Vue: Resolving Uncaught TypeError - n.getClientRects is not a valid function

How do I convert this jquery code into Vue.js format? // Scale the cell item. $(document).on('click','.cell-item a', function(event) { event.preventDefault() var $this = $(this) console.log($this) // [a] - correct v ...

The CSS stylesheet is not being applied to the components in Next.js

I am currently facing an issue with styling my React/Next component. Despite trying to apply a stylesheet to enhance its appearance, the CSS doesn't seem to be taking effect. Could someone please provide guidance on how I can resolve this? I have att ...

Tips for preserving login session continuity following page refresh in Vuejs

signInMe() { this.$store.dispatch('setLoggedUser', true); this.$store.dispatch('setGenericAccessToken', response.data.access_token); this.errorMessage = ""; }, (error) => { if (error) { th ...

Angular and Bootstrap 5 combine to create a dynamic multi-item carousel featuring animated slide transitions and embedded YouTube videos

I'm trying to create a multi-item carousel using YouTube videos, and although I have managed to get it working with Bootstrap 5 carousel and cards, the animation when the carousel slides is not as smooth as I would like. The issue seems to be that the ...

Troubleshooting: Issues with Ajax loading page and click functionality in jQuery

Why won't my ajax function properly? Here are the details of my pages. I am using JQuery to monitor click events and implement ajax for loading the responder page. Can you spot any issues in this code snippet? index.php <html> <head ...

Checking the length of user input with JavaScript

To ensure that the user enters at least 4 letters in a text box and show an error if they don't, I need help with the following code. It is partially working but currently allows submission even after showing the error message. I want to prevent subm ...

Insert PHP File into Division Element

Need help loading a PHP file into a div element without removing existing content? Here's an example: $('.Load_Div').load('example.php'); Let's say the Load_Div already has some content that you want to keep, and you want th ...

Detecting if the request in Yii Framework 2.0 is coming from an AJAX GET call

While utilizing Yii framework 2.0, I have implemented an AJAX GET jQuery script that references a function within a controller class. $.get('localhost/website/index', {param: 'xxxx'}, function(returnedData){ // some code here..... ...

Commit the incorrect file name with the first letter capitalized

There seems to be an issue with the git not recognizing the correct filename casing. I have a file named User.js in my workspace, but when checking the git status, it displays user.js instead. Despite repeatedly changing and committing as User.js, the gi ...

Mastering Inter-Composable Communication in Vue 3: A Guide

Composables in Vue documentation demonstrate how small composition functions can be used for organizing code by composing the app. Discover More About Extracting Composables for Code Organization "Extracted composables act as component-scoped servi ...

Error message indicating that Element <> cannot be scrolled into view persisting despite attempting common troubleshooting methods

Currently, I am utilizing Selenium to create a web scraper for downloading multiple podcast episodes from Spreaker. # https://www.spreaker.com/show/alabamas-morning-news-with-jt for i in range(3): print("Click number: {}".format(str(i))) see_mor ...

Choose an option from a dropdown menu using the <span> tag in Selenium

I've been attempting to choose an option from a dropdown list, here is the HTML snippet for reference. Trying to choose 'Health Care' from the dropdown. <span class="select2-container select2-container--default select2-container--open" s ...

What is the best way to change an object into a string in node.js?

Recently, I've delved into the world of node js and I'm eager to create a basic coap client and server using this technology. Successfully, I managed to set up a server hosting a text file, but now I aim to access it from the client. var coap = ...

Tips for accessing the values of fields in React Material UI components

When I console log the object, it shows: { "week":undefined, "name":undefined, "code":undefined } Additionally, is it appropriate to wrap all Material UI components in a form tag and treat the entire code as a form? Here is ...

Can two controllers be used for the main-app and sub-app with the same URL using angular ui-route?

I have implemented two controllers, 'BaseController' and 'SubController', for my main application and sub-application. Both controllers are configured to point to an empty URL. Below is the configuration for the main application:- ang ...

Automatically populate textboxes with database information based on selected combobox item without the need to refresh the page

I would like to implement a feature where textboxes are automatically filled from a database when a user selects an option from a combo box. This can be achieved using jQuery or JavaScript in PHP. Once a user selects an element, the corresponding text sh ...

Utilize getElementsByClassName to dynamically alter the appearance of specific elements upon triggering an event

I'm attempting to utilize onmouseover="document.getElementsByClassName().style.background='color'" in order to switch the color of all divs with a specified classname to a different color when hovering over another element on the page. ...

Will Node applications be affected as newer versions of their dependencies are released, causing them to stop functioning properly?

I'm currently following an online tutorial on web development topics, and the associated code is hosted on GitHub. After downloading it, I noticed that the directory structure looks like this: E:\dev\angular-components-step-1>dir Direct ...

It appears that AsyncStorage.getItem() is not functioning as expected

Whenever I attempt to use AsyncStorage.getItem() to assign a value, I encounter difficulties retrieving it afterwards. let tokenData = null; const getData = async () => { let token; try { token = await AsyncStorage.getItem('token'); ...