Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to make xpath work for an HTML block of code

I have this html block:

<td><a href="#" class="">   <i class="far fa-times mr-1"></i>Cancel</a></td>

I wrote the following xpath to find this Cancel anchor, but this is not working. What am I doing wrong?

//a[contains(text(), 'Cancel')]

The following xpath works but i need to learn what am i doing wrong in the above one:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

//i[@class='far fa-times mr-1']/..

>Solution :

//a[contains(text(), 'Cancel')]

Issue: The reason above XPath expression is not working is because, above XPath looks for an anchor (<a>) element containing the text Cancel. However, the <a> element in your HTML snippet doesn’t directly contain the text Cancel. Instead, it contains an <i> element and then the text Cancel.

Solution: Change the XPath expression as below:

//a[contains(., 'Cancel')]

XPath explanation: The . represents the current node, so contains(., 'Cancel') checks for the presence of "Cancel" text within the <a> element and all of its descendants.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading