XPath for link in an html page by using the label of the link
I have a link that can not be identified in my html page shown below:
开发者_如何学运维<a href="/somthing/confirm_delete_somthing?&id=12">Delete this monitor</a>
What is the XPath to identify the link by the label Delete this monitor
?
something like //a[@label="Delete this monitor"]
?
Replace @label
with text()
which represents a text value of a node.
//a[text()="Delete this monitor"]
For cases where the text is on another line, such as
<a>
Delete this monitor
</a>
you can use normalize-space()
function which removes starting and trailing spaces:
//a[normalize-space(text())="Delete this monitor"]
Use:
//a[. = "Delete this monitor"]
or
//a[normalize-space() ="Delete this monitor"]
This will select the node even when it is something like the following:
<a href="/somthing/confirm_delete_somthing?&id=12">Delete <strong>this</strong> monitor</a>
In contrast, the expression from currently accepted answer does not select such a node.
精彩评论