Giymose
Giymose

Reputation: 201

Get text value from div using selenium web driver in java

I have this html :

<div class="b-datalist__item__subj">
hello
<span class="b-datalist__item__subj__snippet">hello world</span>
</div>

And I want to get only "hello" text, without "hello world". I write this code:

subject = this.driver.findElement(By.xpath("//div[@class = 'b-datalist__item__subj']")).getText();

But in console output I see "hellohello world". How can I fix it?

Upvotes: 1

Views: 4534

Answers (2)

SiKing
SiKing

Reputation: 10329

You will have to "subtract" the text inside the span from the complete text in the div. Something like:

String outsideText = driver.findElement(By.xpath("//div")).getText();
String insideText = driver.findElement(By.xpath("//span")).getText();
String yourText = outsideText.replace(insideText, "");

Adjust the XPaths as necessary for your situation.

Upvotes: 2

noor
noor

Reputation: 3004

use the below as xpath:

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

Upvotes: -1

Related Questions