Extract the li list with xpath

1

Hello,

I'm developing an application with java + selenium I have the second web element:

WebElement results = ( new WebDriverWait( driver, timeout  ) ).until( ExpectedConditions.visibilityOfElementLocated( By.id( "local" ) ) );

Where id = local is a div that contains an html list of type:

<div id="local">
<ul>
<li>
   <span>texto</span>
</li>
<li>
   <span>texto</span>
</li>
<li>
   <span>texto</span>
</li>
</ul>
</div>

I wanted to extract the text from all. I do not know how to get the li's list with xpath from the div in WebElement. Then I wanted to get a list in java with all li, to iterate and so get every span.

Any ideas how to do it? I do not know how to go get xpath the names I want.

Is it possible to extract all span with xpath at once?

Thank you!

    
asked by anonymous 30.05.2017 / 20:49

1 answer

1

Via xpath you will be able to access all the spans by the expression:

//div[@id = 'local']/ul/li/span

This expression says that it will enter the div with id local go through each ul , for each ul it will go through each li and for each li it will return every span .

That said, you can search in selenium using findElements and passing By.xpath , or the way you're using you can use visibilityOfAllElementsLocatedBy

The following is the code below:

String xPath = "//div[@id = 'local']/ul/li/span";
List<WebElement> results = (new WebDriverWait(driver, timeout))
        .until(ExpectedConditions
                .visibilityOfAllElementsLocatedBy(
                        By.xpath(xPath)
                )
);

Now in results you will have the list of all span's now if you want the list of all li's just change the xpath by deleting /span To take the% test, I like to use freeformatter

    
30.05.2017 / 23:51