0

I want to get the texts associated with the xpath "//p[@class='list-group-item']".

Below is the HTML snippet:

<div _ngcontent-c3="" class="list-group bg-trans mar-no">
  <p _ngcontent-c3="" class="list-group-item" style="border-bottom: 1px dotted #ddd;">
    <span _ngcontent-c3="" class="badge badge-pink" id="stat_2" style="background-color: rgb(225, 124, 167);">0</span>
    In Action
  </p>
  <p _ngcontent-c3="" class="list-group-item" style="border-bottom: 1px dotted #ddd;">
    <span _ngcontent-c3="" class="badge badge-purple" id="stat_3" style="background-color: rgb(152, 98, 145);">5</span>
    Completed
  </p>
  <p _ngcontent-c3="" class="list-group-item" style="border-bottom: 1px dotted #ddd;">
    <span _ngcontent-c3="" class="badge badge-dark" id="stat_4" style="background-color: rgb(59, 65, 70);">0</span>
    Closed
  </p>
  <p _ngcontent-c3="" class="list-group-item" style="border-bottom: 1px dotted #ddd;">
    <span _ngcontent-c3="" class="badge badge-defadivt" id="stat_8" style="background-color: rgb(227, 232, 238);">0</span>
    Long Term Solution
  </p>
  <p _ngcontent-c3="" class="list-group-item" style="border-bottom: 1px dotted #ddd;font-weight:bold">
    <span _ngcontent-c3="" class="badge badge-defadivt" id="stat_8">5</span>
    Total
  </p>
</div>

In result I want "In action", "Completed", "Closed", Total".

Below is the code I have written so far.

List<WebElement> lst= driver.findElements(By.xpath("//p[@class='list-group-item']"));
List<String> strgs = new ArrayList<String>();

for(WebElement e1 : lst){
    strgs.add(e1.getText());
}
System.out.println(strgs);

Output I got:

[
  0 In Action, 
  5 Completed,
  0 Closed, 
  0 Long Term Solution, 
  5 Total
]
yong
  • 13,357
  • 1
  • 16
  • 27
Mansi
  • 63
  • 3
  • 11
  • Can you please describe what actual result you get from your code. Is the result incorrect or you are not sure about is the idea correct? – kamentk Oct 05 '18 at 12:31
  • OutPut: [0 In Action, 5 Completed, 0 Closed, 0 Long Term Solution, 5 Total] – Mansi Oct 05 '18 at 12:37
  • String count = driver.findElement(By.xpath("//div[@class='list-group bg-trans mar-no']/p).getText(); try with this xpath might be your xpath is wrong – Dhru 'soni Oct 05 '18 at 12:51
  • Are you trying to get just the Element text (

    ), excluding any sub element text ()? Try this post: https://stackoverflow.com/questions/39741076/how-to-use-selenium-get-text-from-an-element-not-including-its-sub-element/39752671

    – dank8 Oct 05 '18 at 13:10
  • @Dan Yes, It helped. Thanks! – Mansi Oct 05 '18 at 13:59
  • Possible duplicate of [How to use selenium get Text from an element not including it's sub-element](https://stackoverflow.com/questions/39741076/how-to-use-selenium-get-text-from-an-element-not-including-its-sub-element) – dank8 Oct 05 '18 at 14:03

1 Answers1

0

If you want to populate the values on text node then it doesn't supported by the Selenium. e.g. your locator would be //p[@class='list-group-item']/text()[2] to find only the <p> tag text exclude <span>

An alternative way is you can use below JavascriptExecutor code to perform the task :

List<WebElement> lst= driver.findElements(By.xpath("//p[@class='list-group-item']"));
List<String> strgs = new ArrayList<String>();

for (WebElement element : lst) {

    JavascriptExecutor js = (JavascriptExecutor) driver;
    String sourceName = (String) js.executeScript("return arguments[0].childNodes[2].textContent", element);
    strgs.add(sourceName.trim());
}

System.out.println(strgs);

Here return arguments[0].childNodes[2].textContent is javascript code which returns the second textnode of <p> which is expected text you need.

NarendraR
  • 7,577
  • 10
  • 44
  • 82