For the below HTML code
<table class="table" id="Table1" xpath="1">
<thead>
<tr class="table-header"> </tr>
</thead>
<tbody>
<tr class="table-row">
<span><i data-icon="" class="icon fa fa-pencil fa-1x" id="l2_0-0-Edit_Icon"></i></span>
</tr>
<tr class="table-row">
<span><i data-icon="" class="icon fa fa-pencil fa-1x" id="l2_0-0-Edit_Icon"></i></span>
</tr>
<tr class="table-row">
<span><i data-icon="" class="icon fa fa-pencil fa-1x" id="l2_0-0-Edit_Icon"></i></span>
</tr>
<tr class="table-row">
<span><i data-icon="" class="icon fa fa-pencil fa-1x" id="l2_0-0-Edit_Icon"></i></span>
</tr>
<tr class="table-row">
<span><i data-icon="" class="icon fa fa-pencil fa-1x" id="l2_0-0-Edit_Icon"></i></span>
</tr>
</tbody>
</table>
<pre>
Suppose I need to click Edit Button on 4th row .. I can use following code. (This code is working fine)
Driver.FindElement(By.XPath("//table[@id='Table1']//tr[3]//i[contains(@id,'Edit_Icon')]")).Click()
The Problem is Edit Button may not always exist on 1st, 2nd or 3rd row. (Its dynamic based on diff conditions)
<table class="table" id="Table1" xpath="1">
<thead>
<tr class="table-header"> </tr>
</thead>
<tbody>
<tr class="table-row">
<span><i data-icon="" class="icon fa fa-pencil fa-1x" id="l2_0-0-Edit_Icon"></i></span>
</tr>
<tr class="table-row">
</tr>
<tr class="table-row">
<span><i data-icon="" class="icon fa fa-pencil fa-1x" id="l2_0-0-Edit_Icon"></i></span>
</tr>
<tr class="table-row">
<span><i data-icon="" class="icon fa fa-pencil fa-1x" id="l2_0-0-Edit_Icon"></i></span>
</tr>
<tr class="table-row">
</tr>
</tbody>
</table>
Hence i am trying to follow below code, and then change "3" as per need. (I am trying to do is find the 3row and within that i am trying to find Edit Icon). But its not working.
How i can i write an Xpath that always clicks on the row i wanted, irrespective of other rows has "Edit" button or not?
Driver.FindElement(By.XPath("//table[@id='Table1']")).FindElements(By.XPath("//tr"))[3].FindElement(By.XPath("//i[contains(@id,'Edit_Icon')]")).Click()