When working with a <table>
element, I encountered the need to search for its descendants and direct descendants while avoiding wading through nested tables. The elements I seek lack reasonable IDs, so specific selectors are essential:
<html>
...
<table id="tbl">
<tbody>
<tr>
<td>
<div>foo</div>
</td>
</tr>
...
<tr><td><button class="btnDefault"/></td></tr>
</tbody>
</table>
...
</html>
To simplify and possibly speed up the process, caching the <table>
element seems like a good idea:
var table = driver.FindElement(By.Id("tbl"));
var div = table.FindElement(By.CssSelector("> tbody > tr:first-child > td:first-child > div"));
var button = table.FindElement(By.CssSelector("> tbody > tr > td > button.btnDefault"));
The issue arises when attempting to use standard CSS syntax in the second and third queries from the cached <table>
element. This is due to CSS not inherently supporting searching from a specific scope. Is there an alternative approach that could serve as the root of the query?