Finding the count of childNodes within a div using Selenium

I've been grappling with this issue for the majority of today; I'm trying to tally up the number of childNodes within a parent div. It's essentially mimicking a list where each childNode represents a row that I want to count. The HTML structure is as follows:

div<@class="list "> 
   div<@id="list-item-01">
   div<@id="list-item-02">
   div<@id="list-item-03">
   div<@id="list-item-04">
   div<@id="list-item-05">
   ...
</div>

I've mainly been attempting to utilize the getEval() function in Selenium using some JavaScript.

Here are a couple of examples that have not worked:

String locator = "xpath=//div[contains(@class,'list')]";
String jscript = "var element = this.browserbot.findElement('"+locator+"');";
       jscript += "element.childNodes.length;";

String locator = "xpath=//div[@class='list']";
String jscript = "var element = this.browserbot.findElement('"+locator+"');";
       jscript += "element.childNodes.length;";

I am confident that the element exists and my xpath is accurate because when I tried using .isElementPresent, it returned true. Therefore, there seems to be an issue with Selenium and divs.

I also experimented with using document.evaluate() in my JavaScript command, but that approach was just as unsuccessful.

Answer №1

Why not consider using the getXPathCount function instead? You can try something like this:

getXPathCount("//div[contains(@class, 'list ')]/div[contains(@class, 'list-item-')]")

This should be a solution to your problem.

Check out the .Net documentation here

For Java documentation, click here

Answer №2

These dynamic divs are generated using a variable for iteration such as $i and $i++.

Once the divs are printed, the value of $i is added to a hidden field.

If there are 3 divs created, $i will equal 3, and the hidden field will be set to 3.

You can then retrieve the value of the hidden field using javascript.

Alternatively, you can explore these resources:

http://api.jquery.com/parent/ http://api.jquery.com/children/

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

The carousel feature results in the screen jumping to the top of the page when switching slides

View GitHub for Minimum Reproducible Example (Images are not displayed in this example, but it does not affect the issue being discussed) Challenge: The screen automatically scrolls to the top of the page every time the slide index changes. This occurs w ...

The element.find() function in Angular struggles to locate input elements nested within other elements

I have been working on a directive that has the following template (simplified): <table> <tr> <td> <input type="text"/> </td> <td> <inpu ...

Trouble with Ruby and Selenium: Struggling to identify visible elements in a list

Trying to figure out how to identify which elements in a list are visible has been quite the challenge for me. The application I'm working with features variable menu dropdowns where each row shows different options based on specific parameters, even ...

Tap and conceal feature on a mobile website

I seem to be facing a JavaScript challenge. On the desktop version of my website, I've managed to create a functionality where hovering over a button reveals some text, and clicking anywhere else on the site hides the text. However, I'm now stru ...

Steps to initiating Firefox browser using GeckoDriver and Selenium

Having trouble launching the Selenium web browser. I can open Firefox but having issues with Google. Can someone help me with this error? package selenium1; import org.openqa.selenium.WebDriver; //import org.openqa.selenium.chrome.ChromeDriver; import or ...

Toggle visibility of a div with bootstrap checkbox - enforce input requirements only if checkbox is marked

Lately, I've been facing a strange issue with using a checkbox that collapses a hidden div by utilizing bootstrap. When I include the attribute data-toggle="collapse" in the checkbox input section, the div collapses but it mandates every single one o ...

Obtain the Zero-width non-joiner character (‌) using the innerHTML attribute

I am attempting to retrieve a &zwnj; using the innerHTML method The desired output should be This section contains a zero-width‌&zwnj;non-joiner, a non-breaking&nbsp;space &amp; an ampersand However, the current output is: This part c ...

Selenium Remote Driver causes Firefox to crash on Mac OS X when invoked

After setting up the Selenium Remote Driver on my Mac OS X 10.8.5 system and using Firefox 43.0.4, I encountered a problem when executing this code: #!/usr/bin/perl use strict; use warnings; use Selenium::Remote::Driver; use Selenium::Firefox; my $firef ...

The attempt to add a note with a POST request to the /api/notes/addnote endpoint resulted in a

I'm facing an issue while trying to send a POST request to the /api/notes/addnote endpoint. The server is returning a 404 Not Found error. I have checked the backend code and made sure that the endpoint is correctly defined. Here are the specifics of ...

Adjust the height of a div using jQuery once the AJAX call retrieves the data

There is a div that always matches the height of the adjacent div, depending on the content loaded in it. This setup was functioning properly until I implemented a search feature using jQuery. Now, when I perform a search, the element used in the jQuery ca ...

Is it possible to use the inheritance type for a function parameter derived from an object?

When working with Angular, I have been using TypeScript for some time now. Previously, I used to set function parameter types in a hardcoded way, as shown below: login(email: string) {} However, I recently tried to inherit from another object and changed ...

The Flux Router in React Native

I am diving into the world of React Native and currently working on creating a practice app. The issue I'm facing is with the routing functionality in my project. At the moment, I have three main components: the app itself, the router component, and o ...

"Exploring the features of next-auth server-side implementation in the latest version of Next.js,

Is it possible to utilize next-auth in a Next.js 14 application router to access user sessions and display page responses using SSR? If so, what steps need to be taken? ...

Incorporate personalized buttons into the header navigation for each view using VueJS

In my VueJS project, I attempted to include custom buttons in the sub navigation menu which could be customized for each view. Initially, I tried adding a simple property to the main data element, but that didn't yield the desired results. Then, I cam ...

Troubleshooting a JavaScript error when using expected_conditions with Selenium and Python: Issue with the right-hand side of the 'instanceof' operator not being recognized as an

Recently, my selenium/python code that used to function perfectly is now throwing an error message: Error: Right-hand side of 'instanceof' is not an object I'm unsure whether this issue stems from changes in the angular application or if it ...

Unable to perform a POST request and send JSON data using AJAX with jQuery at the

It seems like there may be a server issue causing this problem, and unfortunately, I don't have access to our server to troubleshoot. I was hoping that someone else might have a solution or could help me understand the root cause of the issue. The is ...

Incorporating AngularJS into JSP for Seamless Integration

As part of our application upgrade, we are transitioning from JSP to AngularJS one module at a time. One challenge we face is transferring user data from JSP to AngularJS (specifically index.html). We aim for a smooth transition where invoking the Angular ...

"Discovering the method to access the most recent clicked event data across the entire JavaScript scope

function retrieve_data(){ var information = $(this).data(); // this is undefined console.log(information.crm); } $(document).on('click', '#add_info', (function() { var info = $(this).data(); // this works console.log(info.c ...

When trying to retrieve an XML file that contains an escaped ampersand, a jQuery AJAX call is throwing an error

Before sending text input to a server to be stored in a MySQL database using XML, I first escape the "&" characters: var copyright = copyright.replace(/&/g,"&amp;"); The wrapped XML data block is then sent to the server through jQuery's ...

A useful tip for jQuery users: learn how to prevent the context menu from appearing when a text box is

Is there a way to prevent the context menu from appearing in jQuery when the text box is disabled? The right-click disable functionality works well in all browsers except for Firefox. Please note that the right-click disable functionality works when the t ...