title: Selenium: Find CSS selector dynamically
body: <p>I am new to selenium and I am writing tests for an existing web app. I am trying to select an option from a dynamically generated list. The following HTML is an example of the list:</p>
  
  <pre><code>&lt;table id="selectChannelForm"&gt;
  &lt;tbody id=""&gt;&lt;/tbody&gt;
  &lt;tbody id=""&gt;
  &lt;tr rowtype="container"&gt;
  &lt;td colspan="3" class="second"&gt;
  &lt;ul&gt;   
      &lt;li&gt;
          &lt;a href="selectChannel.php?selectedChannelId=103"&gt;
          &lt;span&gt;Sales Channel&lt;/span&gt;
          &lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
          &lt;a href="selectChannel.php?selectedChannelId=108"&gt;
          &lt;span&gt;Demo channel&lt;/span&gt;
          &lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
          &lt;a href="selectChannel.php?selectedChannelId=112"&gt;
          &lt;span&gt;Finance Channel&lt;/span&gt;
          &lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
          &lt;a href="selectChannel.php?selectedChannelId=121"&gt;
          &lt;span&gt;HR Channel&lt;/span&gt;
          &lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
          &lt;a href="selectChannel.php?selectedChannelId=156"&gt;
          &lt;span&gt;Management Channel&lt;/span&gt;
          &lt;/a&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>I need to select one of these channels for a test, but the problem is that I can't do a direct CSS or XPath reference because both the tbody and the channel list changes based on certain criteria. How would I use either the channelId or span text to select the correct option? The following is a direct CSS reference to the fourth option.</p>
  
  <p><code>WebElement channelBtn = driver.findElement(By.cssSelector("#selectChannelForm &gt; tbody:nth-child(2) &gt; tr &gt; td &gt; ul &gt; li:nth-child(4) &gt; a"));</code></p>
  
  <p>Is it at all possible to put some logic in these statements? Or some other means of selecting an <code>nth-child</code>?</p>