Discovering the dimensions of a disabled attribute within a table using XPath in Selenium with Java

I'm attempting to determine the number of columns in a specific table, but some are disabled - I'd like to know if it's possible to get the count without including the disabled ones (only counting the visible columns).

As you can see in the image attached, certain tds are disabled.

The command I've tried using is:

driver.findElements(By.xpath("//*/table[@id='TABLE NAME]/tbody/tr[2]/td[@style='display:none;']")).size()

The issue is that the style='display:none;' may not work for future tables if the value for disabled tds differs. Thank you in advance.

Answer №1

If you want to only select the elements that are visible, you can achieve this by using a combination of not and contains in your XPath query:

driver.findElements(By.xpath("id('table-id')/tbody/tr[2]/td[not(contains(@style,'display:none'))]")).size()

Alternatively, you can also use a CSS selector to achieve the same result:

driver.findElements(By.cssSelector("#table-id > tbody > tr:nth-child(2) > td:not([style*='display:none'])")).size()

Another approach is to filter out the elements that are not displayed by utilizing the WebElement::isDisplayed method:

driver.findElements(By.cssSelector("#table-id > tbody > tr:nth-child(2) > td"))
  .stream().filter(WebElement::isDisplayed).count()

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

Retrieve each JTextField in a specific sequence

I often utilize Netbeans for GUI development, incorporating numerous JTextField elements. My goal is to iterate through each one and set the text to sequential numbers, starting from 1. private javax.swing.JTextField txt1; private javax.swing.JTextField t ...

What is the best way to animate my logo using JavaScript so that it scales smoothly just like an image logo

I dedicated a significant amount of time to create a unique and eye-catching logo animation for my website! The logo animation I designed perfectly matches the size of the logo image and can be seamlessly integrated into the site. The issue arises when th ...

Moving the sidebar from the app component to a separate component in Angular results in a situation where the sidebar does not maintain full height when the page is scrolled down

Within my Angular application, I have implemented a sidebar as a separate component. Previously, the content of the sidebar was housed within the main app component's HTML page, and it functioned properly by taking up the full height of the page when ...

Alignment of text in a stationary element's location

I'm working on implementing a feature for an online shop where new items are added to the cart, and I want to display the number of new items added. However, I am facing a challenge in centering the text that displays the number, especially when the n ...

How about determining the map size based on Divine proportions?

Is there a way to make the map at https://codepen.io/sinanelms/pen/MqdNNY responsive to screen size changes? I attempted the following code but was unable to achieve the desired outcome. <style type="text/css"> body{ background:#fff;} #map ...

"Safari is not displaying the element's height correctly when set to 100

I am facing an issue where I have a child div vertically centered under a parent div with a specific height. The child div is assigned the height:100% property, but it seems to be not working correctly on Safari only. To see more about this problem, you ca ...

What is the best way to ensure one div expands to full width while simultaneously hiding another div on the page?

Hey there, I'm looking for a way to expand the width of a clicked div while making the other div disappear from the screen simultaneously. It should also be toggleable, so clicking the button again will shrink the div and bring back the other one. For ...

How to transform a string property into a numeric property using Gremlin

I am currently dealing with a graph that has timestamps stored as string properties g.V().order().by('timestamp', '10') This is causing issues because sorting strings is different from sorting numbers. String sorting : 1, 10, 2 Numeri ...

Having trouble triggering the Save button through By.className even though the className is present in the div

Having Trouble Clicking on the Save Button with By.className When the Element has a Different Class Name in the Webpage Code <div class="popupFooter"> <div align="center"> <input id="Preview-btn" class="btn-primary previewDetaile" type="bu ...

Exploring the jungle. Cursor acting strange while dragging

I am currently working on implementing drag-and-drop functionality in my project, utilizing slip.js from slip.js. To enhance the cursor during dragging, I have assigned class="draggable" to each draggable <tr>. The CSS code for this class is: .drag ...

Application experiences a crash upon attempting to resume from the background

My app experiences crashes because the static values are deleted when the application is in the background for a long time or when many different apps are opened. It seems like the Android OS is removing data from my app, causing it to crash. Is there any ...

Why is the div element within the container not automatically occupying 12 columns, but rather only 1?

I'm struggling to comprehend the bootstrap grid system, as explained in Murach's .Net book. Please take the time to read through the entire post. I am aware of the solution (<div class="col-lg-12">Col x</div> works), but I am curious ...

Steps to fix the issue of "Plugin prefix 'surefire_report' not found in current project or plugin groups"

I recently started using Maven for automating a website. When running my test cases, I encountered an error stating "No plugin found for prefix 'surefire_report' in the current project and in the plugin groups." Here is a screenshot of my pom fil ...

Material-UI organizes its Grid Items vertically, creating a column layout rather than a horizontal row

I'm struggling to create a grid with 3 items in each row, but my current grid layout only displays one item per row. Each grid item also contains an image. How can I modify the code to achieve a grid with 3 items in a row? Here's the code snippet ...

Navigating through Ruby on Rails' index routes

I have set up an index for the object "request". Each request has an address and a body. I configured the index to display the address as a link, with the intention of directing users to the show page of that specific object. However, when I click on the l ...

Having trouble retrieving specific elements with Python, Selenium, and Beautiful Soup

I could really use some help with this code to extract the specific data I need from a single page. Despite trying various selenium techniques and implementing a time delay, I can't seem to pinpoint the issue. Strangely, no error messages are appeari ...

Issue with Restful Web service - HTTP Status 404 - Unable to find the requested resource

When attempting to use a restful web service example, I encountered an HTTP Status 404 error stating that the requested resource is not available. Below are the details of my code. If you need any additional information, please let me know: Web.xml < ...

Updating div visibility based on form content in PHP

I am struggling to find a way to filter a page filled with divs based on form input to determine whether they should be displayed or not. The layout is akin to a collection of articles, where each div showcases an image and some text that links to the comp ...

Determining the total number of current connections using JavaScript, jQuery, and Selenium WebDriver

I need your assistance as I've encountered a roadblock. In my automated tests using Selenium WebDriver + Java, I rely on the following code snippet to check for active background AJAX connections: private boolean hasNoActiveConnections() { retur ...

Discovering the previous sibling using xpath in Selenium: A guide

Having some trouble clicking on the checkbox after using siblings. Can anyone help me figure out what's wrong with it? Code: checkbox1 = driver.find_element_by_xpath("td/nobr/a[text()='192.168.50.120']/../preceding-sibling::td/input[@class ...