Reputation: 377
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
Reputation: 14135
Here is another option to get the last span of each td.
//td//descendant::span[last()]
Upvotes: 1
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