Unable to employ the executescript method in Selenium with multiple arguments

I am currently working on automating a website to create a list through input values in a text field and setting them by clicking outside the field. Due to slow performance with Internet Explorer, I am using Selenium webdriver with IE. The sendKeys method is not efficient so I am exploring the executescript method.

My attempt at this approach is shown below, however, it is not functioning as desired. I would appreciate any suggestions from those who have experience in this area.

Here is the code snippet:

//Creating an array of values for the list
String values[] = {"18","25","60","71"};

//Identifying the WebElement for entering values
WebElement searchField = driver.findElement(By.xpath("//label[text()='New value:']/parent::div//input[@type='text']"));

JavascriptExecutor myExecutor6 = ((JavascriptExecutor) driver); 

//Iterating through the values array to input each value individually as required by the UI
for(int i=0; i<values.length; i++){
      System.out.println(values[i]);
      Thread.sleep(3000);
/*The following section attempts to input the value at index 'i' into the designated text field indicated by searchField */
/*There seems to be a JavaScript error that I cannot troubleshoot*/

      myExecutor6.executeScript("var valueToWrite = values[i].toString();"
+"arguments[0].value = valueToWrite ",searchField, values[i]);

      Thread.sleep(3000);

//Simulating a mouse click outside the text field to finalize the value entry
driver.findElement(By.xpath("//label[text()='Values:']")).click();
Thread.sleep(3000);
}

Answer №1

I made some changes to the code, but it was still not working as expected. It seems that in JavaScript, it is necessary to focus on the element to resolve the issue. arguments[0].focus();

Thank you for your assistance.

Here is the revised code snippet:

   String[] values = array.clone();
   String iterationNumber;

   JavascriptExecutor myExecutor = ((JavascriptExecutor) driver);

   for(int i=0; i<values.length; i++){
      System.out.println(values[i]);
      Thread.sleep(1000);
      iterationNumber = values[i];
      myExecutor.executeScript("arguments[0].focus();"+"arguments[0].value = arguments[1];",driver.findElement(By.xpath("//label[text()='New value:']/parent::div//input[@type='text']")), 
              iterationNumber);
      Thread.sleep(1000);
      driver.findElement(By.xpath("//label[text()='Values:']")).click();
      Thread.sleep(1000);
      driver.findElement(By.xpath("//label[text()='New value:']/parent::div/div/div[2]/button")).click();
      Thread.sleep(1000);
   }       
}

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

Using Jquery to Retrieve the Attribute Value from an HTML Element

I am currently developing an interactive online quiz application that is organized by chapters, with each chapter having its own quiz. Users can navigate through the chapters via a menu and start quizzes by clicking on a button after loading the chapter co ...

How can I show the table row as an inner table row when the first field is identical?

<tr ng-model="check" ng-repeat="orderBook in orderBookDetails| orderBy: 'orderBook.no'" value='check=$scope.orderBookDetails.sowNo'> <td ng-if='check!=orderBook.no'>{{orderBook.no}}</td> <td>{{order ...

Performing a search through a string array and updating a specific portion of each string

I am faced with an array containing a long list of Strings, and my goal is to filter out all the strings that begin with 'INSERT ALL' and replace the number inside the parentheses with the string ' NULL' Here is the original array: le ...

Leveraging composer to enhance the organization of my dependencies

As I organize my files for my PHP-based website, I've turned to composer for assistance. One issue I've encountered is loading front-end dependencies like jquery into my pages, specifically the index.php. In the past, I manually specified each js ...

Invoke a router inside Node.js once a route has been triggered

I am working with ExpressJS and integrating the Auth0 API for authentication, along with ReactJS on the client side. Due to some limitations of the Auth0 API that I discussed with their team, I have implemented a workaround where I send updated user detail ...

Exploring the use of functions in the setup method of Vue 3

I'm currently working on a simple app and utilizing mock-json-server to mimic http requests. Within my project, I have defined a function designed to retrieve the necessary information: import { ref } from 'vue' const getScores = () => ...

Ways to incorporate my python script into a different file using visual studio code

I am currently using Python with Selenium in Visual Studio Code. My goal is to import another Python class called driverScript located within the executionEngine module, specifically in the file named DriverScript. I have attempted to import it as shown be ...

Guide on creating a menu that remains open continuously through mouse hovering until the user chooses an option from the menu

I have a unique scenario where I am working with two images. When the mouse hovers over each image, two corresponding menu bars appear. However, the issue is that when the mouse moves away from the images, the menu disappears. Any suggestions on how to im ...

What is the best way to keep a checkbox unchecked after clicking cancel?

I'm working on a bootbox script that triggers a customized alert. I need the checkbox that triggers the alert to be unchecked when the user clicks cancel. Here is the checkbox when it's checked <div class="checkbox"> <label> ...

Unpacking a nested array with GSON

I am encountering difficulties deserializing the JSON data available at . Despite my efforts, an error persists. Below is the code snippet I have been working on: Gson gson = new Gson(); String json = readHTTPS(new URL("https://mtgox.com/code/data/getD ...

Can you explain the contrast between EJS's render() function and renderFile() method?

const express = require('express'); const ejs = require('ejs'); const app = express(); app.engine('ejs', ejs.renderFile); app.get('/', (req, res) => { res.render('index.ejs', { ...

JavaScript allows users to create a single string by merging multiple strings positioned at various points

Let's say we have 3 strings: s1 = "ab", s2 = "cd", s3 = "ef". The task is to form a new string by merging s1, s2, and s3. The twist here is that the user has the freedom to choose the positions of these 3 strings. For example: s1 - position 3; s2 - ...

Using AJAX in a loop presents a challenge: What is the best way to initiate an ajax request for every item in an array?

Hey there! I'm a new member of the StackOverflow community and I could really use some assistance. My current challenge involves performing an inverse geocode to retrieve addresses from coordinates. I have a functional URL that works with ajax, but I ...

Converting Markdown to HTML using AngularJS

I'm utilizing the Contentful API to retrieve content. It comes in the form of a JSON object to my Node server, which then forwards it to my Angular frontend. This JSON object contains raw markdown text that has not been processed yet. For instance, t ...

Having trouble with Next.js environment variables not being recognized in an axios patch request

Struggling with passing environment variables in Axios patch request const axios = require("axios"); export const handleSubmit = async (formValue, uniquePageName) => { await axios .patch(process.env.INTERNAL_RETAILER_CONFIG_UPDATE, formVal ...

Discovering the smallest, largest, and average values across all properties in an array of objects

Given an array of objects with varying values, the task is to determine the minimum, maximum, and average of the properties in that array. For example, consider the following array: const array = [{ "a": "-0.06", "b": "0.25", "c": "-0.96", ...

Updating values within a nested JSON file during iteration in JavaScript

I've been looking everywhere for a solution to my problem with no luck... I have a complex nested json object like this: var obj = { a: 1, b: 2, c: { a: 1, b: 2, c: {a: 'hello', b: 'HowAreYou?'} } }; someValue = 'hello'; ...

Frontend experiencing issues with Laravel Echo Listener functionality

I have recently developed a new event: <?php namespace App\Events; use Illuminate\Broadcasting\Channel; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Broadcasting\PresenceChannel; use Illuminate&bs ...

How can I generate a dummy JSON response using Backbone Fetch?

Exploring Backbone and looking for a way to simulate the response of a .fetch() call within a model without using a testing library or connecting to an external service. If the setting in the model is this.options.mock === true, I'd like to use an in ...

Troubleshooting a problem with data retrieval in Hygraph and Next JS

I'm currently facing an issue while attempting to fetch data from Hygraph in a fresh Next JS application. The error message I'm encountering is: Error: Cannot read properties of undefined (reading 'map'). As a beginner in both technolo ...