title: Dynamically Generate CSS Selector From Table Value
body: <p>We have a website that is built upon a ton of tables. Each cell within the rows is clickable. I am working on a way to dynamically build the cssSelector info by providing the table name and value I want to click on. I am getting close (I think).</p>
  
  <p>Using the practice table at <a href="http://toolsqa.com/automation-practice-table/" rel="nofollow">ToolsQA</a>, say I want to build the cssSelector for the value "Taiwan".</p>
  
  <p>It's cssSelector is: <code>.tsc_table_s13 &gt; tbody:nth-child(4) &gt; tr:nth-child(3) &gt; td:nth-child(2)</code></p>
  
  <p>I am iterating through the table and have successfully been able to enter the cell using the value ("Taiwan") I specified, however, I'm not sure how to get the value of the row and column it is currently on.</p>
  
  <p>Here is the code I am using so far:</p>
  
  <pre><code>driver.get("http://toolsqa.com/automation-practice-table/");
  
  String table = ".tsc_table_s13 &gt; tbody:nth-child(4)";
  String cellValue = "Taiwan";
  
  getCell(table, cellValue);
  
      // Get the cell of a particular value
      public static void getCell(String table, String value) throws IOException{
  
          // Grab the table 
          WebElement tableName = driver.findElement(By.cssSelector(table)); 
  
          // Now get all the TR elements from the table 
          List&lt;WebElement&gt; allRows = tableName.findElements(By.tagName("tr")); 
  
          // And iterate over them, getting the cells 
          for (WebElement row : allRows) { 
              List&lt;WebElement&gt; cells = row.findElements(By.tagName("td")); 
  
              // Print the contents of each cell
              for (WebElement cell : cells) {
  //              System.out.println(cell.getText());
                  if (cell.getText().equals(value))
                  {
                      String cellValue = table + " &gt; tr:nth-child(" + row. + ") &gt; td:nth-child(" + cell + ")";
                      System.out.println(cellValue);
  
                  } // end if text equals
  
              } // end for loop for cells
          } // end for loop for all rows
  
      } // end getCell function
  </code></pre>