The for loop in Selenium WebDriver seems to be malfunctioning

I am encountering an issue with my page where I need to input scores for 16 different rows that are automatically generated. When I try to insert scores into each row using a for loop, the code works fine for just one row but fails when I attempt to run it for more than one row.

Within the code, inside the for loop, the first cssSelector is for a button that prompts for scores for two tests, followed by a save button.

To iterate through each row, I have utilized the variable "i" from the for loop along with the corresponding cssSelector.

Main code:

(Sample Java code intentionally omitted)

Error:

(Sample error log intentionally omitted)

Sample HTML Code:

(Sample HTML code intentionally omitted)

Is there a straightforward solution for this issue?

Answer №1

Here's a solution that avoids using CSS selectors: driver.findElement(By.xpath("html/body/div[3]/div[2]/div[2]/div[1]/div/div[5]/div["+i+"]/div/div[1]/div[4]/button")).click();

Answer №2

It appears that the issue you are experiencing is due to an invalid selector being used. The problem lies in your attempt to increment a variable inside the selector using (i+1), but because everything within the quotes is considered as a string, the computation does not occur. To resolve this, you can concatenate strings like this "div:nth-child(" + (i+1) + ")" (as shown in a concise example). Alternatively, you can utilize String.format method such as

String selector = String.format("div:nth-child(%d)", i+1);

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

Struggling to switch from using ChromeDriver to WebDriver in Selenium WebDriver with Java? Need help overcoming this issue?

I'm encountering an issue with this code import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class Fijan { public static void main(String[] args) { // TODO Auto-generated method stub System.setPro ...

Converting complex JSON structures to Java objects with Jackson's dynamic nested parsing

i'm struggling with parsing json data I have a json string that contains nested elements and I need to convert it into a java object. The structure of the string is quite complex and dynamic, so I'm wondering how to efficiently handle this using ...

Encountered an issue: "Element not interactable" while attempting to input keys [search_bar.send_keys(course_name)] into the YouTube search bar using Selenium in

Despite trying numerous solutions from StackOverflow, I am still facing an issue. I am attempting to input a course name into the YouTube search bar using Selenium in Python. This functionality was working perfectly before, but now I encounter an error whe ...

`Can you explain the significance of selenium chromeDriver's port?`

When only specific ports (53, 443, 80) are open as per company policy, the Selenium ChromeDriver may encounter issues with port configuration. In such cases, manually setting the port to 4444 and adding it to the firewall exceptions can help resolve startu ...

Having difficulty accessing the link button in an email template using Selenium WebDriver

How can I use Selenium WebDriver to identify and interact with links in email body templates? I've been working on automating a process where I need to click on a link within an email body to complete a registration. However, when using a public mail ...

Encountering an "inaccessible module" error when trying to install Selenium in Eclipse using Maven

Currently, I am facing an issue while attempting to integrate the Selenium 4 library into Eclipse using Maven in an existing modular project. Upon adding the selenium-java dependency, I encountered multiple errors including: The type org.openqa.selenium.B ...

Instead of being viewed in the browser, the CSV file is being downloaded

I'm currently using Jhipster and have a function generated by Jhipster to open files in the browser. However, I'm facing an issue with this function when it comes to opening CSV files - instead of opening in the browser, they are being downloaded ...

Discovering the process of parsing a nested JSON object within a JSON object using Java

As a newcomer to Java, I am struggling to parse the following JSON and retrieve the value of "zip-code" as "526262". How can I accomplish this? This is the JSON data: { "id": "6fffdfdf-8d04-4f4e-b746-20930671bd9c", "timestamp": "2017-07-21T03:51: ...

Is it possible to verify the absence of an error icon when valid data is provided in Angular testing?

Currently, I am utilizing the Selenium webdriver for Angular Automated testing. My scenario involves displaying a tooltip icon with an error message if the data provided is invalid. If the data is valid, the tooltip icon does not show up. Although, I have ...

Tips for displaying personalized error messages from your REST API in a JavaScript client

I'm utilizing a Java REST API that has been generated using swagger. If the client is unauthorized, I am sending custom error messages in response. public Response collaborationCollabIdDelete(Integer collabId, SecurityContext securityContext, Str ...

E2E tests for Internet Explorer using Selenium and Protractor

Looking to integrate e2e tests into our CI build process, I have successfully added them for Chrome and Firefox. However, I want to include tests for various versions of Internet Explorer as well. How can this be accomplished in the build process on Linux/ ...

When utilizing the find_elements_by_xpath function in selenium to locate text within a web element, it consistently retrieves the text from the initial web element discovered

Here's a visual representation of the inspector: https://i.stack.imgur.com/u2AZM.png I am trying to extract the text from elements containing the class "info" and having the style "background-color: initial;". Below is the code I am currently using: ...

My Python script utilizing Selenium is failing to execute

Why is the loop not functioning correctly after opening the initial element based on the xpath? I keep receiving the following exception: raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Una ...

The statement within [AfterStep] does not establish the TestError property in ScenarioContext.Current

During the [AfterStep] section of my testing, I encounter a failure: [AfterStep] public void StopOnUnexpectedBrowserLogs() { Assert.Fail("sth sth"); } Following this, during the [AfterScenario] phase, I aim to take action when a test fails: [AfterSc ...

Activate specific settings in User-Agent Switcher and Spoof Timezone in order to load a customized Firefox user profile

I am currently running Linux Mint 20 and utilizing the User-Agent Switcher and Manager as well as Spoof Timezone extensions with Firefox. My goal is to load my current Firefox user profile and set a Chrome 99.0.7113.93 (Windows) user agent using Selenium. ...

Tips for filtering data in JSON results within a Java servlet

I need to filter records by gender that are in a JSON response. I have a method in my DAO class that returns JSON data, so I want to parse the contents to filter by gender type. String results = bedsBean.allBedsInJson(); System.out.println(results); Th ...

Guide on utilizing Thymeleaf for dynamic updates in a table

My code includes a segment like this: <div class="ui-table"> <div class="items"> <div class="justify-content-end d-flex"> <span class="text- ...

Sending an array of objects within another array to an Android device - here's how

Having trouble with this code showing Invalid JSON. Any suggestions for an Android-friendly alternative? Proposed Format:--> { "supplyType":"O", "subSupplyType":"1", "subSupplyDesc":"TESTDESCRIPTION", "docType": "BIL", ...

Using Selenium Webdriver to initiate the play function of a video by clicking on the control

Is there a way to play the video on this page by clicking the play button? I noticed a 'playpause' control in the JavaScript, but I'm unsure how to activate it. <div id="videoModal" class="reveal-modal expand open" style="display: block ...

Struggling to find element using Java in Selenium WebDriver - need assistance

I am facing a challenge where I need to select and delete a dynamically generated row in a table using Java in Selenium WebDriver. The issue is that the ID of the row changes every time a new row is added, so I'm unsure how to access it programmatical ...