Using Selenium WebDriver to retrieve the row number that is visible within a table

As I write a test script using Selenium WebDriver and Java for a page containing a table with multiple rows and columns, I face the challenge of dealing with hidden rows alongside displayed ones. With around 1800 total rows, only seven are visible on the page. Determining which row is actually visible using XPath in a for loop has proven to be time-consuming. Additionally, each row has a dynamic dropdown option with an ID that changes based on the row number (e.g., testid_0 for row 1, testid_1 for row 2). Is there a more efficient way to identify the visible row numbers without resorting to lengthy loops? Perhaps through JavaScript executed by Selenium to pinpoint the displayed rows faster?

Answer №1

To exclude hidden tr elements, utilize precise xpath filtering technique:

//table/tbody/tr[not(contains(@style,'display: none;'))]

Answer №2

If you want to retrieve the number of visible rows in a table on a webpage, you can implement the following code snippet:

int rowCounter = 0;
List<WebElement> tableRows = driver.findElements(By.xpath("//table//tr"));
for(WebElement tableRow: tableRows){
  if(tableRow.isDisplayed())
    rowCounter++;
}
System.out.println("The total count of visible rows is: "+ rowCounter);

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

Discovering a collection of rows in a grid with Selenium

In my framework, I am working on a scenario where I need to determine the row count and select one or multiple rows from a grid. All rows have the same class name and attributes, but each row has a different id. https://i.sstatic.net/sKG3v.png I tried wr ...

Tips for dynamically altering the @Test method within TestNG

I am currently facing a challenge with designing my test automation framework, specifically regarding incorporating TestNG and reporting/logging. My framework is built using Selenium, where I retrieve data (method names) from an excel file. Within my main ...

Modifying an item in a list using React and Redux

I am facing an issue with my table that consists of 50 rows, each representing an item from a list. When I click on a checkbox within a row, I want to mark that specific item as selected. However, the problem is that clicking on any checkbox causes all 50 ...

Improving the retrieval of API data using personalized React hooks when searching by modifying keywords

I'm just starting out with React Hooks and recently wrote a small code snippet that displays a list of courses to users. This code includes two main components, CourseList and Course, as well as a custom hook called useCourseList. Here's the code ...

How can I display random images in the gallery?

I am looking for a gallery that is similar to the one in this provided link: The codes seem too complex for me to figure out on my own. Thank you ...

Discover the Practical Utility of Maps beyond Hash Tables in Everyday Life

I am currently attempting to explain the concept of Maps (also known as hash tables or dictionaries) to someone who is a beginner in programming. While most people are familiar with the concepts of Arrays (a list of things) and Sets (a bag of things), I ...

Making a single variable object as opposed to using multiple variables

In order to improve the code structure, I am looking to consolidate all properties into a JavaScript object instead of using multiple variables: // Method 1 // This method gives an error as _inp cannot be accessed by input_value // Uncaught TypeError: Can ...

Create a shader in ThreeJS without the need to include a Geometry in the scene

Currently, I am experimenting with drawing shapes or geometric figures in ThreeJS r128 using only shaders. The traditional approach in this library involves creating a mesh with a geometry associated with it, and then applying a shader using the ShaderMat ...

Encountered a problem while trying to install my package from the npm registry - Error number 4058. The main.js file cannot be found in the

After creating a sample package and publishing it on the npm registry under my personal npm account with the name newtestmay, I encountered an error when trying to install it using the command npm install newtestmay. The error message below is extracted fr ...

Discovering where memory is being consumed in Java Heap Dump can be done by identifying the specific objects or classes that are taking up space, such as io.netty.buffer.ByteBufUtil and byte

My Spring Boot project's memory consumption has been steadily increasing over the past few days. Initially, when I uploaded the jar file to the AWS server, it only utilized 582 MB of RAM (with a maximum allocated RAM of 1500 MB). However, each day the ...

Enhance your websites' search functionality with jQuery autocomplete using AJAX

I am attempting to implement dynamic autocomplete values for a text box. Here is my solution: echo json_encode($res) 0: {type_name: "name1"} 1: {type_name: "name2"} 2: {type_name: "name3"} 3: {type_name: "name4"} 4: {type_name: "name5"} Below is the co ...

Which approach is preferable: using inheritance or accepting parameters, and what are the reasons for this choice?

Imagine having a scenario where there is an Address class with various parameters such as street number, address line 1, address line 2, country etc. Then there is a Person class with parameters like name, email, and each person also has an address. In th ...

I am facing a challenge with AngularJS where I am unable to navigate between pages using the

I'm having issues with my route file app.js. Whenever I click on any link or button, it redirects me to books.html. What could be the mistake I'm making? var myApp = angular.module('myApp', ['ngRoute']); myApp.config([&apo ...

Exploring the concept of inheritance in JavaScript and Angular programming

Currently, I am working on a project called "hello world" and it involves two HTML pages named "configuration.html" and "add configuration.html". Each page has its own controller defined as follows: angular.module('MissionControlApp').controller ...

Is there a way to compare CSV records with those in SQLite to verify if they match?

I am currently dealing with a CSV file that contains 10 million records. My task is to verify if all the values in this file match those in an SQL database. It's not enough for me to simply get a true or false result; I also need to identify how many ...

Error: Attempting to access a property of an undefined object resulting in TypeError (reading 'passport')

I am currently working on a project that requires me to display user profiles from a database using expressjs and mongoDB. However, I have encountered an issue and would appreciate any solutions offered here. Here is the code from my server: const express ...

The chaotic world of Android WebKit versions 2.x and 3.x

I have been working on an Android app and my goal is to make it compatible with Android versions 2.2 and above. Currently, due to issues with the WebView control, I am restricted to targeting Android 4.0 and higher. The app primarily uses HTML, CSS, Java ...

Retrieve an array field across various documents and combine the elements to form a single object

Consider a scenario where there is a users collection. Each user document includes an array of posts called posts. The goal is to query this collection as follows: Retrieve 2 posts, starting at index Nstart and ending at index Nend, from each user in the ...

What is the best way to update Mongoose models?

Here is the task at hand: Delete all existing documents from the model Save new documents for the model This is an example of how this can be accomplished: Model.remove().then(function () { Model.save(new Model({ *some valid data* }); } ...

Invisible and Unrestricted automatic playback

Why is auto play muted in both Firefox and Chrome? How can we code it so that browsers don't block it? Here's the code I'm using: <audio id="audio1" src="https://notificationsounds.com/storage/sounds/file-sounds-1217-relax ...