TransWikia.com

How to get XPath for the text after the tags

Software Quality Assurance & Testing Asked by ferosh khan on December 6, 2021

I Need to get XPath for the following, to get the text after the <b> tags

<li class="SpacedOut">
<b>Name 6:</b> 
    "ABC" 
<b>Name 1:</b>
    "BCD"
<b>Name 2:</b>
    "EFG" 
<b>Name 3:</b>
    "HIJ" 
 <b>Name 4:</b>
    "KLM"
 <b>Name 5:</b>
    "NOP"    
 <br>

I need to get the following texts from the above HTML, please help how to set XPath to get the following text

Required output: “ABC” “BCD” “EFG” “HIJ” “KLM” “NOP”

3 Answers

You can try this XPath, this should make it:

"//li[@class='SpacedOut']/text()[normalize-space()]"

Good luck!

Answered by Svetlana Levinsohn on December 6, 2021

Try this: //li[@class='SpacedOut']/text() You'll be able to return it into a list if you're using Selenium

Answered by Renato Lopes on December 6, 2021

I'm not sure if this is possible using just XPath, but there may be some advanced stuff I'm unaware of.

This is fairly simple using regular expression matching though:

        driver.get("https://1a9f9143-5107-46d6-85df-3b5fc765cd60.htmlpasta.com");

        WebElement list = driver.findElement(By.className("SpacedOut"));
        Pattern pattern = Pattern.compile(""\w+"");
        Matcher matcher = pattern.matcher(list.getText());

        while(matcher.find()) {
            System.out.println(matcher.group());
        }

The regex may need adjusting if your real text is substantially different from what you supplied. I would suggest researching Java regular expressions in that case.

Answered by anonygoose on December 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP