Selenium – For loop to navigate to the web element list and verify list item.

Step 1: Store item of the list in a List using findElements

List<WebElement> searchResultlist = Driver.driver.findElements(By.xpath("//span[@class='document-name']"));

Step 2: Use for loop to traverse in the list and verify the text using IF condition.
To make sure input by the user and text from the application are in same case, convert both the strings in UPPER case and match.

for (WebElement resultList : searchResultlist){
     System.out.println(resultList.getText());
     if (resultList.getText().toUpperCase().contains(DocumentNumber.toUpperCase())){
         resultList.click();
         break;
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s