Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Comment with xPath by Comment-Text

Tags:

xpath

How can I select the next node after an defined comment? I know the text of the comment and need the next nodes.

Best regards Christoph

like image 803
chmav Avatar asked Apr 07 '10 19:04

chmav


People also ask

How do I comment out XPath?

Comments are allowed in an XPath expression, wherever nonessential whitespace is allowed. Comments do not affect expression processing. A comment is a string that is delimited by the symbols (: and :) . The following example is a comment in XPath: (: This is a comment.

What is text () function in XPath?

XPath text() function is a built-in function of the Selenium web driver that locates items based on their text. It aids in the identification of certain text elements as well as the location of those components within a set of text nodes. The elements that need to be found should be in string format.

How do I search for text in XPath?

So, inorder to find the Text all you need to do is: driver. findElement(By. xpath("//*[contains(text(),'the text you are searching for')]"));

What does node () do in XPath?

node() matches any node (the least specific node test of them all) text() matches text nodes only. comment() matches comment nodes. * matches any element node.


1 Answers

Supposing the comment is a child of the current node, and the comment's text is "comment-text", then the following XPath expression selects the first element (sibling) that follows the comment node:

comment()[. = 'comment-text'][1]/following-sibling::*[1]

If we want the following element to be selected (regardless if it is sibling of the comment or not), the corresponding XPath expression is:

comment()[. = 'comment-text'][1]/following::*[1]
like image 110
Dimitre Novatchev Avatar answered Sep 18 '22 11:09

Dimitre Novatchev