4

i am new with Selenium and try to get all items in list. The code is:

<div id="books">
  <ul>
    <li>Aw</li>
    <li>Ax</li>
    <li>Ay</li>
  </ul>
</div>

I want to get all items and check all of them starts with "A"

Thanks for help.

sametbilgi
  • 582
  • 2
  • 7
  • 29

1 Answers1

5

To get all the items, we can use 'findElements' method like below:

List<WebElement> items = driver.findElements(By.xpath("//div[@id='books']/ul/li"));

Here items is a List containing the desired elements (i.e. all <li> elements).

We can then iterate through this list, and check if the text of these items starts with 'A':

for(WebElement e: items)
{
    System.out.print(e.getText());
    if(e.getText().startsWith("A"))
    {
        System.out.println("  ==> starts with 'A'");
    }
    else
    {
        System.out.println("  ==> does NOT start with 'A'");
    }
}
Mukesh Takhtani
  • 852
  • 5
  • 15