Using Selenium in JavaScript, extracting content from a specific class tag

Currently, I am using Selenium Webdriver in JS to scrape data from a website. My goal is to determine the number of <tr> tags that contain exactly three children.

<tr>
 <td>One</td>
 <td>Two</td>
</tr>
<tr>
 <td>One</td>
 <td>Two</td>
 <td>Three</td>
</tr>
<tr>
 <td>One</td>
 <td>Two</td>
</tr>
<tr>
 <td>One</td>
 <td>Two</td>
 <td>Three</td>
</tr>
<tr>
 <td>One</td>
 <td>Two</td>
 <td>Three</td>
 <td>Four</td>
</tr>

The end result: There are 2 tags containing only three child nodes of <td>.

I'm looking to create an xPath expression for this task but need some guidance. Can someone assist me with this? Thank you!

Answer №1

Below is the XPath you can utilize to target tr elements that contain exactly 3 child td elements:

//tr[count(td)=3]

Answer №2

Give it a shot with this Xpath: Find<WebElement>textElements=driver.findElements(By.xpath("//td[text()='Three']"));

    System.out.println(textElements.size());

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

Adding incremental values to a variable using JavaScript within the framework of jQuery and AJAX

In my JavaScript code that utilizes jQuery and AJAX, I have created a dynamic array containing multiple values for AJAX requests. The array is structured as follows: <script type="text/javascript> var array = Array("y", "y", "x", "y", "y", "y"); fu ...

Ensuring secure JSON parsing in Node.js when encountering errors

When working with node/express and trying to extract JSON from request headers, I want to make sure it's done safely. If the JSON is not valid for some reason, I don't want it to throw a syntax error - instead, I prefer it to just return false or ...

What is the best way to develop interactive components using Google App Scripts for Google Sites?

I am currently working on building dynamic app script components that can be seamlessly integrated into my website, each with unique data for every instance of the script. I initially attempted using parameters but I'm uncertain if this is the most ef ...

What is the best way to extract the singular PDF link from a webpage?

Currently, I am attempting to utilize Selenium in Java to access DOM elements. However, I have encountered an issue while testing the code: Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is n ...

Choosing an option from a dropdown menu using Selenium in Java

Although similar questions have been asked before, I have exhaustively tried all solutions provided in those posts with no success. It seems that there is a unique issue within the HTML code in my case, particularly related to the css class ui-helper-hidde ...

Is there a way to alter the color of options within a select tag when hovering the mouse over them

After searching through multiple websites, I couldn't find a solution on how to change the option background color on hover from blue to green. The screenshot provided below illustrates what I am requesting for in a clearer manner. https://i.sstatic. ...

Combining several JSON objects into a single JSON object using JavaScript

I am in need of merging JSON objects from various sources into a single JSON object using JavaScript. For example, I have two JSON objects with different values that I need to combine and add a new value to the final result. json1= { "b":"t ...

Increasing the upward motion of the matrix raining HTML canvas animation

Recently, I've been experimenting with the Matrix raining canvas animation here and I was intrigued by the idea of making it rain upwards instead of downwards. However, my attempts to achieve this using the rotate() method resulted in skewing and stre ...

Is there a way to streamline the testing of a Silverlight application using the Selenium webdriver?

Currently trying to familiarize myself with Silverlight and Selenium. My search for resources on automating my Silverlight application using the Selenium webdriver has come up short. Any guidance would be greatly appreciated! Thanks in advance, ...

Issues with callback functionality in asynchronous JavaScript function

Attempting to retrieve a result from an asynchronous function in Javascript has proven challenging. After coming across the query: How do I return the response from an asynchronous call?, I endeavored to incorporate the callback solution, yet encountered ...

Challenges with Firebase Firestore Query Index when Using Multiple Filters and Combining Them

I am in the process of developing a website that involves pagination and filtering specific data. if (eventType) query = query.where('general.eventType', '==', eventType); if (price) query = query.where('general.eventTicketPrice&a ...

Error in TypeScript Compiler: When using @types/d3-tip, it is not possible to call an expression that does not have a call

Seeking help to understand an error I encountered, I have read all similar questions but found no solution. My understanding of TypeScript is still growing. I am attempting to integrate the d3-tip module with d3. After installing @types/d3 and @types/d3-t ...

Instead of returning an object, the underscore groupBy function now returns an array

Currently, I am attempting to utilize underscore to create an array of entities that are grouped by their respective locations. The current format of the array consists of pairs in this structure { location: Location, data: T}[]. However, I aim to rearran ...

Numerous input fields available for AJAX auto-complete functionality and name verification

Currently, I am working on implementing a text box that will search through a mysql database and display auto-completed text below the input field. Additionally, I want to include a visual indicator like a confirmation tick or cross to signify whether the ...

VueJS's approach to routing through modular components

I am currently working on a website where I need to set up different category pages using dynamic routes. To achieve this, I am utilizing vue-router and aiming for a single dynamic route that can switch between pages by loading different components. Here ...

Tips for adjusting the font size according to the varying number of characters entered

I am currently facing a challenge with adjusting the font size based on the dynamic number of characters. For example, I have a set of characters with a font-size of 40px, and as more characters are added, the font size should decrease to fit within the av ...

Navigating to a precise location on a webpage with React.js

I want to implement a straightforward action where the page automatically scrolls to a specific position upon loading. However, my attempts to execute this action have been unsuccessful so far. Is there an alternative method to achieve this in a React ap ...

Fixed positioning upon scrolling begins prior to reaching the uppermost point (top div)

Currently, I have implemented a feature where #filter (the white select list in my sidebar) becomes fixed when it reaches the top of the page and stays there while scrolling until it reaches the footer and is released. However, I encountered an issue wit ...

Why is continuous testing an essential component of software development?

Why is continuous testing essential in the development process? Are there specific methods to effectively integrate continuous testing tools with CI/CD tools? Share your insights on writing QA testing code (e.g. using Java with Selenium) and seamlessly i ...

FOUC: Website first displayed without any design elements

My goal is to implement global styles in a Next.js app by importing `.scss` files into `_app.js`. Unfortunately, I am facing an issue where the styles are not being applied on page load, resulting in FOUC (Flash of Unstyled Content) for the initial page r ...