What is the difference between the @textContent attribute, the text() function and the dot (.) in xpath?

Asked

Viewed 18 times

0

Can anyone tell me the difference between the @textContent attribute, the text() function and the dot (.) in xpath? I performed several tests using the findElements(By.xpath) method of Selenium and the Firefox browser, but I could not understand why the results.

The basic HTML I used was the following:

<html>
<body>
    <p id="1">
        <span>A jaqueta vermelha</span>
    </p>
    <p id="2">
        <span>A</span><span>jaqueta</span><span>vermelha</span>
    </p>
    <p id="3">
        <span>A</span> <span>jaqueta</span> <span>vermelha</span>
    </p>
    <p id="4">
        <span>A<br />jaqueta<br />vermelha
        </span>
    </p>
    <p id="5">
        <span>A <br />jaqueta <br />vermelha
        </span>
    </p>
    <p id="6">
        <span><pre>
A
jaqueta
vermelha
</pre></span>
    </p>
    <p id="7">
        <span>A jaqueta</span>
    </p>
    <p id="8">
        <span>A</span><span>jaqueta</span>
    </p>
    <p id="9">
        <span>A</span> <span>jaqueta</span>
    </p>
    <p id="10">
        <span>A<br />jaqueta
        </span>
    </p>
    <p id="11">
        <span>A <br />jaqueta
        </span>
    </p>
    <p id="12">
        <span><pre>
A
jaqueta
</pre></span>
    </p>
    <p id="13">
    <pre>
A
jaqueta
</pre>
    </p>

</body>
</html>

The most illustrative examples among the various xpath I tested were:

  1. "//p[@textContent=text()]"
  2. "//p[@textContent=.]"
  3. "//p[text()=.]"
  4. "//p[normalize-space(@textContent)=normalize-space(text())]"
  5. "//p[normalize-space(@textContent)=normalize-space(.)]"
  6. "//p[normalize-space(text())=normalize-space(.)]"

Briefly, the results obtained were:

  • Queries 1, 2 and 3 did not return Webelement’s, that is, no Webelement was found.
  • Query 4 returned all 13 tags <p> with @id attribute and still 5 instances of Webelement’s without @id attribute.
  • Queries 5 and 6 returned tags <p> of @id 6, 12 and 3, that is, only those containing the tag <pre> as daughter, and still 3 instances of Webelement’s without @id attribute.
  • Webelement’s without @id attribute of queries 4, 5 and 6 return the value '' when gettext() and getattribute("textContent") methods are called.
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.