Understanding xpath with '.' as an attribute

For a few Stackoverflow questions / answers, I find xpath how //div[.='hello']Can someone explain what exactly the role is .in the expression and when to use it?

+4
source share
3 answers

Expression

//div[. = 'hello']

Find all div elements whose string value is "hello". The string value of an element is defined as the concatenation of all these text nodes of the descendants of the element, so all of the following elements will correspond:

<div>hello</div>
<div>hel<i>lo</i></div>
<div>h<i>el</i>lo</div>

text()different is the location path step that retrieves the set of all text node children of the current node context. So

//div[text() = 'hello']

div, node , "" (, = node ) - , , ,

<div>foo bar baz<br/>hello</div>

node - "" .

, , , ,

<div>
  <!-- some comment -->
  hello
</div>

//div[contains(., 'hello')] , //div[contains(text(), 'hello')] ! div node ( , - "" ), XPath 2.0 , , . XPath 1.0 node , , node , , , .

text() , , - ., text(), .

+1

, node.

HTML:

<a ui-sref="main.home" href="#/home" class="">Home</a>

xpath, Home

//a[.='Home']

, //a[text()='Home']

- , //a[.='Home'] /, //a[text()='Home'] , .

0

1) If XPATH contains

./equivalent to current node value. For example: //tbody[@id='tableID']/tr[./td[3][text()='Sample']]---- under tbodywith identifier "tableID", trwith tdwith textSample

2) //div[.='hello']equivalent //div[text()='hello'] For example: //button[contains(.,'Apply changes')]---- The button with the text contains "Apply changes"

-1
source

Source: https://habr.com/ru/post/1544704/


All Articles