Picking specific elements of a cell in HTML table with Selenium

Recently, I have incorporated Selenium into my data collection process for a website that heavily utilizes JavaScript. While I have successfully extracted cells from the table, I am now faced with the challenge of isolating the specific "Individual" strings within each cell. For instance:

One of the cells in the table looks like this:

<tr data-ig="x:360964033.17:adr:0:tag:" tag="" adr="0" type="row">
   <td idx="0" adr="0" type="cell">2014-11-02 21:15:00</td>
   <td idx="1" adr="1" type="cell">AMALT</td>
   <td idx="2" adr="2" type="cell">60007</td>
   <td idx="3" adr="3" type="cell">107115</td>
   <td class="ig9a63765d">1</td>
   <td idx="9" adr="9" type="cell">576833</td>
</tr>

I am looking to individually select the row

<td idx="0" adr="0" type="cell">2014-11-02 21:15:00</td>
. How can I accomplish this? I attempted something like
List<WebElement> allRows = table.findElements(By.tagName("idx=0"));
but it didn't yield the desired results.

My current approach involves fetching the entire table and its cells as follows:

 // Retrieve all TR elements from the table 
         List<WebElement> allRows = table.findElements(By.tagName("tr")); 
         // Iterate over them to obtain the cells 
         for (WebElement row : allRows) { 
             List<WebElement> cells = row.findElements(By.tagName("td")); 

             // Print the contents of each cell
             for (WebElement cell : cells) { 
                 System.out.println(cell.getText());
             }break;
         }

Thank you for any assistance provided.

Update: It appears that the layout of the table has changed dynamically:

<tr data-ig="x:360964033.17:adr:0:tag:" tag="" adr="0" type="row">
      <td>2014-11-04 23:00:00</td>
      <td idx="1" adr="1" type="cell">gasdjjhg</td>
      <td idx="2" adr="2" type="cell">11344</td>
      <td idx="3" adr="3" type="cell">14500</td>
      <td idx="4" adr="4" type="cell">saddasd</td>
      <td></td>
      <td>sdsed</td>
      <td>dsfdsf</td>
      <td class="ig9a63765d">1</td>
      <td></td>
   </tr>

It seems the table layout keeps undergoing changes. What is your advice in handling such scenarios?

Answer №1

In this scenario, the `idx` element is not a tag but an attribute. To target elements based on attributes in Selenium, you can make use of XPath:

table.findElement(By.xpath("//td[@idx=0]"))

Answer №2

Give this script a shot:

Note: Provided the 'data-ig' value remains constant.

    Retrieve allRows = table.findElements(By.xpath("//tr[@data-ig='x:360964033.17:adr:0:tag:']/td"));
    for(WebElement row: allRows){
    System.out.println(row.getText());
    }

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

CSS - font with dashed color styling

I need to display text on a webpage that has the following appearance: https://i.sstatic.net/vFA2A.png The font should have two alternating colors. Can this be achieved? The solution needs to be compatible with all web browsers. ...

Retrieve the tagName element from within the specified class

I am attempting to access and modify the element a inside a specific class. The HTML in question is as follows: <div class="alignleft"> <a href="http://cismdomain.com/articles/page/2/">« Older Entries</a> </div> My goal is t ...

Learning how to effectively utilize TestNG within Selenium WebDriver

Can you provide guidance on utilizing TestNg with Selenium WebDriver? I am a beginner in the field of Selenium WebDriver and would appreciate an explanation regarding its purpose and usage. ...

Retrieve designated data from numerous PDF documents and save to a text document

In my directory, there are numerous subdirectories housing PDF files and more subdirectories with more PDF files. It's quite a chaotic mix of documents. My goal is to extract a specific field from each file and compile the contents into a single text ...

How do I show information from MySQL on an Android app based on the department of the user currently logged in?

I'm in the process of developing an application for staff members (users) working in a factory. The app requires users to log in before accessing its features. Upon logging in, the next page displays a spinner whose value is determined by the user&apo ...

Shadows in Three.js are failing to render

After reviewing all the shadow-related posts here, none of them seemed to apply to my issue or I may have just missed it about 10 times. Check out this link for more information. I wanted to create a fiddle for this problem, but unfortunately, the versio ...

Using JavaScript, adding an element to an array results in the array returning a value of 1

When I try to push a string into an array and then return the array, I am getting unexpected result of "1". Upon closer inspection, it seems that the array is being successfully created but before returning it, only "1" is being returned. Here is the snip ...

Uploading files with a progress bar in NodeJS by utilizing Core NodeJS and the authentic Node approach

Ryan Dahl mentioned that he created NodeJS to address the file upload progress bar issue (https://youtu.be/SAc0vQCC6UQ). During its introduction in 2009, Node utilized technology available at the time before more advanced client-side javascript libraries l ...

Is there a method to prevent data loss on page refresh by persisting data stored in a database?

I'm currently developing a commenting feature for a blog using the latest version of NextJs. The text input collects data and sends it to the 'Vercel' hosted database. I am able to successfully fetch the data from the frontend as expected. ...

When extracting a value from an HTML textarea that contains multiple lines of text inputted by the user, the JSON becomes invalid

Here is the JSON format I am working with: { questionID: int studentAnswer: "String" } The issue I am facing is that the studentAnswer field is currently only capable of holding input from a single line in an HTML textarea. If I input text that spa ...

Pause the image slider using jQuery when the accordion menu is collapsed

Click here to see the demo Can anyone help with making the image slider go blank when all menu items are closed? Also, is there a way to make it stop on the first image if only one image is available, such as posture 3? I'm stuck and could use some ...

Extract the complete HTML information from the weather website

Currently, I am attempting to retrieve weather data from the following website: using this code snippet: try { int i = 0; if (googlefirst3.startsWith("http")) { Document document = Jsoup.connect("https ...

Refreshing the information in the database table

Upon receiving data from the server using ajax, I populate this table: $.each(data, function(i, item) { $('#MyTable tbody').append("<tr>" +"<td>" +data[i].A+ "</td><td>" +data[i].B ...

Conquer the issue of incessant AJAX page refreshing

Currently, I am working on an application where I handle numerous form submissions and utilize ajax to send data to a server running on Node.js. One of the features includes updating a table upon button click, which triggers a spinner to load as an indic ...

Retrieving Data Value from a Website with Python Web Scraping

Introduction Greetings everyone! I am new to the world of Python and web scraping. Currently, I am using a Mac (Sierra) with Jupyter Notebook through Firefox (version 87.0). My goal is to extract various data points from a webpage similar to the one found ...

Struggling with rendering components in REACT?

I'm encountering an issue with rendering the Butcher Shop component. I can't seem to pinpoint what's causing it to be null or undefined. Can someone help me identify the mistake? Nothing is showing up on the DOM and I keep getting this error ...

Navigating the data returned by an AJAX call can be quite perplexing

Every time a link is clicked on the website located at , an AJAX request triggers to load the content automatically. Lately, I have been encountering a perplexing piece of code injected into my response data: <script> var _q = document.createEle ...

Retrieve information from an external JSON file and display it in a jstree

I am trying to pass JSON data to a jstree object from an external file. The code snippet I have does not seem to be working properly. <script> $.jstree.defaults.core.themes.responsive = true; $('#frmt').jstree({ plugins: [" ...

Ways to retrieve a glb model without the loader in Three.js

Currently, I am utilizing a GLTFLoader in threejs along with a glb model initialization. My main objective is to extract the model from the loader for use outside of it. Despite researching numerous topics on this matter, none have provided a solution to m ...

Arranging an Array by Two Distinct Characteristics

I currently have an array that is grouped and sorted based on the "Program" attribute, which is working well. However, I now need to sort by a different attribute (Deliverable) within each group. Is this possible? If so, how can I achieve it? Below is an ...