Hi!
I am trying to understand the scope when using IWebElement.FindElement(By by). My basic question is: does calling the method from an existing Web Element limit WebDriver's search to the descendants of that element, or will it still look over the entire page?
Example html:
<table id="importantTable">
<tbody>
<tr>
<td class="strong">Item 1</td>
<td class="weak">Item 2</td>
</tr>
</tbody>
</table>
<table id="NotImportantTable">
<tbody>
<tr>
<td class="strong">Outlier 1</td>
<td class="weak">Outlier 2</td>
</td>
</tbody>
</table>
Okay, so in my code, I want to retrieve the text from the first table's first column. So I was wondering if the following should work in C#:
IWebElement tableElement = driver.FindElement(By.CssSelector("table#importantTable"));
IWebElement cellElement = tableElement.FindElement(By.CssSelector("td.strong"));
string myText = cellElement.Text;
So I have been working under the impression that cellElement will uniquely locate the "Item 1" cell, because that cell is the only one available in the context of importantTable. However, it appears (at least based on my locator strategies) that the code will fail, because it locates both elements.
I can use other strategies to locate that first element, using css or xpath, but I am curious about the effectiveness or purpose of using context elements to narrow the scope.
Thanks!