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:
<table id="selectChannelForm">
<tbody id=""></tbody>
<tbody id="">
<tr rowtype="container">
<td colspan="3" class="second">
<ul>
<li>
<a href="selectChannel.php?selectedChannelId=103">
<span>Sales Channel</span>
</a>
</li>
<li>
<a href="selectChannel.php?selectedChannelId=108">
<span>Demo channel</span>
</a>
</li>
<li>
<a href="selectChannel.php?selectedChannelId=112">
<span>Finance Channel</span>
</a>
</li>
<li>
<a href="selectChannel.php?selectedChannelId=121">
<span>HR Channel</span>
</a>
</li>
<li>
<a href="selectChannel.php?selectedChannelId=156">
<span>Management Channel</span>
</a>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
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.
WebElement channelBtn = driver.findElement(By.cssSelector("#selectChannelForm > tbody:nth-child(2) > tr > td > ul > li:nth-child(4) > a"));
Is it at all possible to put some logic in these statements? Or some other means of selecting an nth-child
?