Issue
<div class="row">
<div class="row">
<table>
<tbody>
<tr>
<td class="list-text">
<div style="background-color: transparent;">Testing this function</div>
</td>
<td class="list-buttons" rowspan="2">
<a href="javascript:;">Config</a>
<a class="edit-button" href="javascript:;">Edit</a>
<a class="save-button" href="javascript:;" style="display:none;">Save</a>
<a href="javascript:;">Delete</a>
</td>
</tr>
<tr>
</tbody>
</table>
</div>
I need to find the element which contains the text "Testing this function" and click to link "Config".
Can't find a way to do it.
Edited: Found a way to do it:
By.xpath("//div[@class='row']/table/tbody/tr[td/div[contains(.,'Testing')]]/td[2]/a[contains(.,'Config')]")
But maybe someone has a better solution for this?
Solution
If you have to use text, my preference would be;
By.xpath("//div[contains(text(),'Testing')]]//a[contains(text(),'Config')]") .
I prefer using "text()" to "." just for readability. Apologies if "." is actually a better approach which I wasn't aware of.
Also unless you have a specific reason, I would avoid using tightly coupled xpath. E.g. I have not used " td[2]", instead I have said the"a" can be anywhere within in the "div"
Answered By - Robbie Wareham
Answer Checked By - Pedro (JavaFixing Volunteer)