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
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.
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.
So, inorder to find the Text all you need to do is: driver. findElement(By. xpath("//*[contains(text(),'the text you are searching for')]"));
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With