utilizing Java with Selenium WebDriver to manage web tables

     <table class="footable table table-hover table-striped table-bordered" 
     cellspacing="0" cellpadding="6" border="0">
     <thead>
     <tr class="CartBlueHeader">
     <th align="10%">PNR No</th>
     <th width="23%" align="center">Origin</th>
     <th width="22%" align="center">Destination</th>
     <th width="10%">Departure</th>
     <th width="10%">Return</th>
     <th width="10%">Amount</th>
     <th width="15%"/>
     </tr>
    </thead>
    <tbody>
      <tr class="BGLightblue font11">
       <td align="left">   Q2S2SO </td>
       <td align="left">   Dubai Intl Airport </td>
       <td align="left">   Hindustan Airport </td>
       <td align="center"> 30 Sep 17 </td>
       <td align="center">-</td>
       <td align="left"> 608.00 SAR   </td>
       <td align="left">
    </tr>
    </tbody>
    </table>

I'm looking to extract data from this table based on specific column headers. If you have any suggestions on how I can achieve this, please let me know.

Thank you in advance for your help!

Answer №1

Stick to the established sequence

// Locate and retrieve the table 
WebElement table = driver.findElement(By.id("divListView")); 

// Gather all the TR elements within the table 
List<WebElement> allRows = table.findElements(By.tagName("tr")); 

// Go through each row and extract its cells 
for (WebElement row : allRows) { 
    List<WebElement> cells = row.findElements(By.tagName("td")); 

    // Display the content of each cell
    for (WebElement cell : cells) { 
        System.out.println(cell.getText());
    }
}

I trust this information proves beneficial

Answer №2

Introducing the Table Class:

public class CustomTable {

  private WebDriver driver;

  private By baseLocator;
  private By headCellLocator;
  private By bodyRowLocator;
  private By bodyCellLocator;

  private String[] headColumns;
  private List<WebElement> foundRows;

  public CustomTable(WebDriver driver, By baseLocator) {
    this.driver = driver;
    this.baseLocator = baseLocator;
  }

  public CustomTable initialize(By headCellLocator, By bodyRowLocator, By bodyCellLocator) {
    this.headCellLocator = headCellLocator;
    this.bodyRowLocator = bodyRowLocator;
    this.bodyCellLocator = bodyCellLocator;

    this.extractHeadColumns();

    return this;
  }

  private void extractHeadColumns() {
    List<WebElement> headCells = this.driver
        .findElement(this.baseLocator)
        .findElements(this.headCellLocator);

    for(WebElement headCell : headCells) {
        ArrayUtils.add(this.headColumns, headCell.getText().trim());
    }
  }

  public CustomTable getRows() {
    this.foundRows = this.driver
        .findElement(this.baseLocator)
        .findElements(this.bodyRowLocator);

    return this;
  }

  public CustomTable filterRowsByColumnValue(String columnName, String columnValue) {
    int columnIndex = ArrayUtils.indexOf(this.headColumns, columnName);
    
    getRows();
    
    List<WebElement> filteredRows = new ArrayList<WebElement>();

    for (WebElement row : foundRows) {
      String cellText = row.findElements(this.bodyCellLocator)
          .get(columnIndex)
          .getText()
          .trim();

      if (cellText.equals(columnValue)) {
        filteredRows.add(row);
      }
    }

    this.foundRows = filteredRows;

    return this;
  }

  public String[] getColumnValues(String columnName) {
    int columnIndex = ArrayUtils.indexOf(this.headColumns, columnName);
    String[] columnValues = null;
    String cellText;

    for (WebElement row : foundRows) {
      cellText = row.findElements(this.bodyCellLocator)
          .get(columnIndex)
          .getText()
          .trim();

      ArrayUtils.add(columnValues, cellText);
    }

    return columnValues;
  }
}

CustomTable customTable = new CustomTable(By.cssSelector('body .customtable'));

customTable.initialize(By.cssSelector('thead th'), By.cssSelector('tbody tr'), By.cssSelector('td'));

customTable.getRows().getColumnValues('Product Name');

// or
customTable.filterRowsByColumnValue('Category', 'Electronics').getColumnValues('Price');

Answer №3

The TableDriver extension for WebDriver, which can be found at https://github.com/jkindwall/TableDriver.Java, offers the ability to locate specific data within a table using contextual clues. For instance, in the scenario presented above, if you needed to retrieve the "Departure" information corresponding to a particular "PNR No", you could achieve this by executing the following code snippet.

Table table = Table.create(driver.findElement(By.cssSelector("table.footable.table")));
String departure = table.findCell("PNR No= Q2S2SO ", "Departure").getElement().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

Executing multiple test classes with TestNG while utilizing page factory

I have developed two classes for locating elements on two web pages within the same package, named LoginPage.java and AddEmployee.java. Additionally, I have created two test classes in a separate package to correspond with the aforementioned classes, named ...

Guide to accessing a mobile version website with Selenium WebDriver

I wrote a code snippet to open the mobile version of Facebook on my Desktop using Firefox by tweaking the user-agent. @Test public void fb() { FirefoxProfile ffprofile = new FirefoxProfile(); ffprofile.setPreference("general.useragent.over ...

I am looking to create a for loop that will automate the process of clicking on multiple links and navigating back to the previous page

Below is the code I have written to achieve a specific task: my_list = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//table[@border='1']//a"))) for option in my_list: option.click() WebDriver ...

VBA automation is unable to retrieve innerText (or innerHTML, value)

I've encountered a problem with getting innertext this time. Despite trying various alternatives, I have not been successful. Firstly, I am using Selenium on VBA with chromedriver. The source website is "[" This website operates within an Iframe. Aft ...

Finding specific text within an HTML tag using Selenium can be achieved by implementing certain techniques

Can someone help me extract the price of 234,40 € from the HTML code below using selenium? <a class="js-sku-link sku-link" title="Samsung Galaxy Watch4 Classic Bluetooth Stainless Steel 46mm Αδιάβροχο με Παλμογράφο (Black)" dat ...

Why does the 'Click Method' keep throwing a 'Stale Element Exception'?

Why does the Click Method keep returning a "Stale Element Exception"? Sometimes the method will successfully click on the intended link:- public @FindBy(xpath = ".//div[@class='category_menu']//a[text()='Supercars »']") WebElement ...

Is there a way to automate the uploading of captcha images to this solving service using Python?

Currently, I am working on a Python program with the goal of solving captchas on a website. My plan is to integrate 2captcha into the solution. Using Selenium, I have managed to write a Python script that fulfills all required tasks except for solving the ...

Encountering an InvalidSessionIdException from selenium.common.exceptions: Description: session id is not valid

When attempting to find the CheckboxClick web element from inside a function call defined in a try block, I am encountering the Error of selenium.common.exceptions.InvalidSessionIdException: Message: invalid session-id. However, when placing the code outsi ...

retrieve a compilation of all the events linked to a specific DOM element

When using Firefox, you can view events associated with each element in the Inspect Element of developers tools. https://i.stack.imgur.com/bKOK0.png I am looking to create a list of elements and their associated events automatically, ideally using seleniu ...

Selenium 4 in Java: Tricks to Bypass Detection and Successfully Log into Gmail with Firefox - Troubleshooting the

I'm looking to streamline the login process for Gmail using Selenium 4, but I keep encountering the following message: Screenshot Is there a way to bypass this detection system? I've attempted all the available solutions online, but none have w ...

Tips for changing to a frame and retrieving an element within it

Looking to access the element containing the text Deprecated within the website "" using selenium in the Chrome browser. WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(driver.findElement(B ...

VBA - Selenium does not support pasting dates on Chrome browser

When using internet explorer, I was able to change the date on a web page with the code (code1) provided. However, after switching to Chrome and utilizing Selenium (as in code 2), the date section on the web page only partially changes. For example, if th ...

Automating POST API requests with Selenium by incorporating a JSON header

Is it possible to automate REST API with Selenium (Java) if it includes header and body parts in JSON form? ...

Encountering an issue while trying to execute a selenium application in the terminal: (Error message: ImportError - Unable to

Although I am a beginner in Python and Selenium, I have encountered an issue: When I execute my Python script using the IDLE (pressing F5), Selenium works perfectly (it opens Firefox, navigates to a website, and performs tasks). However, when I attempt to ...

Java Selenium WebDriver code for logging into Gmail

I have encountered an issue with my code designed to log in to GMail, where it stops at the email id entry and displays the following error message: Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method" ...

Selenium technique for navigating iframes without specific identifiers

I encountered an issue while trying to switch to an iframe on a page I am scraping. The ID has been removed from the iframe, making it difficult for me to switch to it. Unfortunately, I have not been able to find any helpful documentation on this matter. I ...

Ways to obtain the final day of the previous month

Attempting to utilize automated bots here, I am in need of retrieving the last day of the previous month. The bot performs accurately on the current month during the first run; however, when transitioning to the next month, the date retrieved from this cod ...

Is there a potential security measure that can deactivate automation processes?

I'm currently developing a Python script using the Selenium Chrome Webdriver to automate tasks on this specific Swedish website: . My main focus is automating the login process, however I have encountered some persistent errors such as: selenium.comm ...

How do I input text into a Google Doc using an XPATH or element that is difficult to manipulate?

I'm currently working on a Python code that is designed to automatically open a Google Doc and input a specific set of combinations for a predetermined length. While I've successfully created the code to navigate to my Google Docs and open a new ...

Connection to Firefox at 127.0.0.1:7055 on CentOS could not be established within the 60-second time frame

Encountering the following error message: unable to obtain stable firefox connection in 60 seconds (127.0.0.1:7055) when executing this code: require 'watir-webdriver' require 'headless' headless = Headless.new headless.start begin ...