Home > Enterprise >  Get The Element "Text" from xml in selenium Java
Get The Element "Text" from xml in selenium Java

Time:10-25

I have the follwing nodes in XML :

<div  xpath="1">
Par Aymen Test - [email protected]
<br>
<time datetime="2022-10-11T16:01:20 02:00">Publié le 11/10/2022 à 16h01</time>
</div>

I want to get the text element following the class "publishing" Knowing that I can't use the content of text because it changes.

I tried with :

WebElement MyText = driver.findElement(By.xpath ("//div[@class='publishing']"));
System.out.println(MyText.getText());

But I get :

Par Aymen Test - [email protected]
Publié le 11/10/2022 à 16h01

I need just :

Par Aymen Test - [email protected]

CodePudding user response:

You can get the total text and then remove from it the text content of child elements.
This should work:

WebElement parentEl = driver.findElement(By.xpath ("//div[@class='publishing']"));
String totaltext = parentEl.getText();
List<WebElement> children = parentEl.findElement(By.xpath (".//*"));
for(WebElement child : children){
    totaltext = totaltext.replace(child.getText(), "");
}
System.out.println(totaltext);
  • Related