Selenium Webdriver - Fetching Table Data
html-table, selenium, uitableview, webdriver
Solution
You could look for all children of tr element without differentiating between td and th. So instead of
List<WebElement> cells = row.findElements(By.tagName("td"));
I would use
List<WebElement> cells = row.findElements(By.xpath("./*"));
Problem
I want to fetch data from tables in UI. I know about looping through rows and columns using "tr" and "td". But the one the table I have is something like this: ``` <table> <tbody> <tr><td>data</td><th>data</th><td>data</td><td>data</td></tr> <tr><td>data</td><th>data</th><td>data</td><td>data</td></tr> <tr><td>data</td><th>data</th><td>data</td><td>data</td></tr> </tbody> </table> ``` How can I make my code generic, so that the occurrence of "TH" in middle can be handled. Currently, I am using this code : ``` // Grab the table WebElement table = driver.findElement(By.id(searchResultsGrid)); // Now get all the TR elements from the table List<WebElement> allRows = table.findElements(By.tagName("tr")); // And iterate over them, getting the cells for (WebElement row : allRows) { List<WebElement> cells = row.findElements(By.tagName("td")); for (WebElement cell : cells) { // And so on } } ```