Using a for loop with the JavaExecutor in Selenium WebDriver

Hello everyone, I am new to using the Selenium webdriver and I'm struggling with implementing a for loop while utilizing the JavaScript executor. Can someone help me convert the code below into a loop structure? The code snippet involves changing the index value after div[6], as shown in this line: driver.findElement(By.xpath("//*[@id='unfreezedGridBody']/div[6]/div[5]/div/div/span[3]"));

Any assistance would be greatly appreciated.

// Loop through different elements on the page
for(int i = 1; i <= 5; i++) {
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    WebElement dropdowns = driver.findElement(By.xpath("//[@id='unfreezedGridBody']/div[6]/div[" + i + "]/div/div/span[3]"));
    jse.executeScript("arguments[0].scrollIntoView()", dropdowns);
    jse.executeScript("arguments[0].click();", dropdowns);
    Thread.sleep(2000);
    driver.findElement(By.xpath("//li[@data-content='PASS']")).click();
    Thread.sleep(10000);
}

Answer №1

Perhaps this solution will suit your needs

    String xpath = "//[@id='unfreezedGridBody']/div[6]/div[%d]/div/div/span[3]";
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    for(int i=1; i<4; i++){
        WebElement dropdowns = driver.findElement(By.xpath(String.format(xpath,i)));
        jse.executeScript("arguments[0].scrollIntoView()", dropdowns);
        jse.executeScript("arguments[0].click();", dropdowns);
        Thread.sleep(2000);
        driver.findElement(By.xpath("//li[@data-content='PASS']")).click();
        Thread.sleep(10000);
    }

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 to extract the href generated by JavaScript when clicking a button using selenium?

Currently, I am utilizing selenium to extract automated phone reporting data from our Barracuda Cudatel phone system. While the system lacks an API that provides the necessary information, there is a button on the report page with attached JavaScript that ...

Incorrectly aligned highlighted labels are appearing on the Kendo chart category axis item labels

My current project involves using the kendo chart to generate charts, and I need specific labels to show up as bold text. To achieve this, I am utilizing the visual method which allows me to customize labels and render them with createVisual(). However, w ...

Encountering a syntax error while utilizing a JavaScript variable in a jQuery selector for a lightbox feature

I'm currently working on creating a lightbox feature and have reached the stage where I am implementing next and previous buttons to navigate through images. I am utilizing console.log to check if the correct href is being retrieved when the next butt ...

Loop through a JSON array in a React Native application

Encountering an issue while working in react native involving parsing a JSON object and iterating over a nested array. The goal is to list all profiles objects. Tried the code below but it's not fetching the profiles object. How can I print all profi ...

Having trouble getting the desired output while iterating through a JavaScript object in search of a specific key and value

Here is an example of how my data is structured: var obj = { QuestionId: 97, SortOrder: { '9': '1' }, directive: { '1': false, '2': false, '9': true }, data: { '1': '', '2 ...

Issue encountered during selenium connection test (unexpected undefined value as string)

I'm encountering the error: Expected [object Undefined] undefined to be a string and I'm unsure how to resolve it. I'm not sure where to begin looking for the potential error. Firefox opens but doesn't do anything except navigate to th ...

What is the best way to store data from multiple selected rows in different datagrids into a single state?

Programming Languages : JavaScript with React, Redux Toolkit, and Material-UI Issue : My goal is to synchronize the selection of checkboxes across multiple datagrids into one central state Attempts Made : I first attempted to manage checkbox selection fo ...

Utilize puppeteer by passing a function inside page.waitForFunction() for efficient automation processes

Below is the code I am using: function checkDataRefresh(pastAvgGain, currentAvgGain) { if (pastAvgGain !== currentAvgGain) { return true; } else { return false; } } async function fetchInformation(pair, page) { let pastAvgGain = C.AVG ...

Converting a string into a list extracted from a text document

Within a file, the data (list) is structured as follows: [5,[5,[5,100,-200],200,-400],300,-500] Upon reading this file in an angular application, the contents are converted into a string object like so: "[5,[5,[5,100,-200],200,-400],300,-500]" Is there ...

Preserving alterations to media files using an image cropper

I've created a special controller for an image cropping tool that pops up over a media item in the content section. Right now I can manipulate the crops in the overlay, but when I click "Save Crops", the changes don't stick. However, if I use the ...

Expanding a JavaScript list using HTML inputs

I have a script where a grid dynamically displays a list of words. Is there a way to populate the word list in the grid using HTML? Currently, I am doing this... var listOfWords = ["mat", "cat", "dog", "pit", "pot", "fog"]; Can additional words be adde ...

Using selenium to identify HTML elements that do not contain a specific CSS property

Here is an example of what the HTML code looks like: Please note that for better readability, I have simplified the classes to unique ones represented by "...". ... <div id=”container”> <div class=”classA1 classA2”> <div cla ...

Steps for opening a link from a jQuery dialog box

I am seeking guidance on how to navigate through a link generated by PHP. Upon clicking the link, a jQuery dialog box should appear, and once the "OK" button is clicked, the code should proceed to follow the PHP-generated link. Below is an excerpt of the ...

Avoiding the "Chrome is being controlled by automated software" message in Firefox while utilizing Selenium

Recently, I've been experimenting with using Selenium in Python to automate tasks like opening Netflix by simply writing a script. However, I've encountered an issue while using Firefox where the browser detects me as a robot. This is frustrating ...

Is there a method to initiate a 'simple' action when dispatching an action in ngrx?

Here's the scenario I'm facing: When any of the actions listed below are dispatched, I need to update the saving property in the reducer to true. However, currently, I am not handling these actions in the reducer; instead, I handle them in my ef ...

Creating a Cubic Bezier Curve connecting two points within a 3D sphere using three.js

I'm currently working on a project where the user can click on two points on a sphere and I want to connect these points with a line along the surface of the sphere, following the great circle path. I have managed to obtain the coordinates of the sele ...

What is the best way to insert a button at the end of the last row in the first column and also at the

I am working on a JavaScript project that involves creating a table. My goal is to dynamically add buttons after the last row of the first column and also at the top of the last column. for (var i = 0; i < responseData.length; i++) { fo ...

Is it feasible to animate a JQuery slider?

Is there a way to animate the slider using jQuery? I want to be able to press a button and have the inner part of the slider move slower (e.g. 300ms) without reacting to mouse clicks and slides. Here is the code: http://jsfiddle.net/gm4tG/5/ HTML: <d ...

Backbone and Laravel - Choose a squad and automatically create users for the selected team

I've recently started exploring backbone.js and have gone through Jeffery Way's tutorial on using Laravel and Backbone. As of now, I have a list of teams being displayed along with their ids fetched from the database. I have also set up an event ...

Changing JSON String into a File in Java

I need help converting an Object to JSON, and then converting it to a File in order to send it to AWS S3 for storage. What is the most efficient way to convert the String for this task? Any suggestions would be appreciated! Below is my code snippet: Str ...