How to get XPath for the text after the <b> tags

  • I Need to get XPath for the following, to get the text after the



    <li class="SpacedOut">
    <b>Name 6:</b> 
    <b>Name 1:</b>
    <b>Name 2:</b>
    <b>Name 3:</b>
     <b>Name 4:</b>
     <b>Name 5:</b>

    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"

  • 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:

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

    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.

Suggested Topics