Tomasz Wojciechowski
Tomasz Wojciechowski

Reputation: 377

How to get text of deepest text?

Sometimes wanted text is for class A, but other times, depper class B appears and then text relocates to deeper class B. How to,using xpath, get text from deepest class with text?

<?xml version="1.0" encoding="UTF-8"?>
<html>
   <td>
      <span class="classA">wanted text in case when classA is deepest one</span>
   </td>
   <td>
      <span class="classA">
         <span class="classB">wanted text in case when classB is deepest one</span>
      </span>
   </td>
</html>

Upvotes: 3

Views: 182

Answers (2)

supputuri
supputuri

Reputation: 14135

Here is another option to get the last span of each td.

//td//descendant::span[last()]

Here is the evidence. enter image description here

Upvotes: 1

user3379369
user3379369

Reputation:

Assuming that classA always exists;

if(driver.findElements("//span[@class='classB']").size() > 0)
    return By.xpath("//span[@class='classB']");
else
    return By.xpath("//span[@class='classA']");

Updated according to the new need. Here is the xpath you are looking for.

//span[not(descendant::span)]

Upvotes: 3

Related Questions