Note that there are some explanatory texts on larger screens.

plurals
  1. POSelenium WebDriver: Help getting specific row data from a table
    text
    copied!<p>I have a table with 12 columns, one of the columns is named "quantity" what I need to do is find out which column is the quantitiy one and then get all of the data from the table for that column. </p> <p>This is some rough code that I have written but at the moment it is just giving me all the data from the table instead of the part that I need.</p> <pre><code>// Get the table name WebElement table = driver.findElement(By.className("compact")); // Get all the Table headers and look for one the element called quantity. List&lt;WebElement&gt; allHeaders = table.findElements(By.tagName("th")); System.out.println("This is the total numbers of headers" + allHeaders.size()); int quantityHead = 0; // Get the column where the quantity is listed for(WebElement header : allHeaders) { quantityHead++; if(header.getText().equalsIgnoreCase("Quantity")) { System.out.println(header.getText()); System.out.println(quantityHead + " is the column number for the quantity"); } } // Now get all the TR elements from the table List&lt;WebElement&gt; allRows = table.findElements(By.tagName("tr")); System.out.println("This is how many allRows: " + allRows.size()); // This is how many items are in the order item table. number of rows - 1 coz top row is the headings int z = allRows.size() - 1; System.out.println("This is how many items are in the table : " + z ); // Work out the cells we need to get by // iterate over all the td for (WebElement row : allRows) { // now we have all the td List&lt;WebElement&gt; cells = row.findElements(By.tagName("td")); for (WebElement cell : cells) { for(int x=0; x &lt;= allHeaders.size(); x++) { int y = x; // if y = the value where the qty header is then output the text of that cell if(y == quantityHead ) { System.out.println(); System.out.println("The quanity is: " + cell.getText()); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload