1

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"

4 Answers 4

0

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.

0

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

0

You can try this XPath, this should make it:

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

Good luck!

1
  • 1
    Also, you can get the specified text(inner html content) instead of using WebElement list //li[@class="SpacedOut"]/text()[normalize-space()][2] This will get the text "BCD" for Other text content change the position from [1] to [5] Feb 24, 2020 at 4:50
0

Here is something you can work with to get the strings you are looking to fetch from the HTML you have shared in the question,

HTML:

<html>
<title>Test</title>

<body>
    <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>
</body>

</html>

Java:

try {
    WebDriver driver;
    System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "//chromedriver");
    driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    driver.get("file:///home/milin/test.html");
    driver.manage().window().maximize();
    Thread.sleep(2000);

    String s = null;
    List<WebElement> allB = driver.findElements(By.xpath("//li/b"));
    int bCount = allB.size();
    for (WebElement b : allB) {
        String wrapperText = driver.findElement(By.xpath("//li[@class='SpacedOut']")).getText().trim();
        String bText = b.getText();
        s = wrapperText.substring(wrapperText.indexOf(bText));
        wrapperText = s.trim();
        wrapperText = wrapperText.replace(bText, "").trim();
        if (wrapperText.contains(" ")) {
            wrapperText = wrapperText.substring(0, wrapperText.indexOf(" "));
        }
        System.out.println(wrapperText);
    }

    Thread.sleep(5000);
    driver.close();
    driver.quit();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Output:

"ABC"
"BCD"
"EFG"
"HIJ"
"KLM"
"NOP"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.