Sort Web elemnets

Hello guys,

I have faced a problem with sorting the web elements.

This is my code:

def static getInsideRemainingPrincipalForAllGroups(String clientNO, String fieldName) {
	//		List<WebElements> elements = DriverFactory.getWebDriver().findElements(By.xpath("//div[@class='sc-bdVaJa sc-iwsKbI sc-fQkuQJ vSwtM']"))
			def groupsFromDB = DataService.getGroups(numberNO)
			KeywordUtil.logInfo("Groups from DB: " + groupsFromDB)
			def myListRemainingPrincipal = []
				WebUI.delay(1)
				if(fieldName == 'Remaining principal') {
					List<WebElements> consumerElements = DriverFactory.getWebDriver().findElements(By.xpath("//div[@class='sc-bdVaJa sc-iwsKbI sc-fQkuQJ vSwtM']"))
					for(int i = 0; i < groupsFromDB.size(); i++) {
						consumerElements.get(i).click()
						WebUI.delay(10)
						def mainRemainingPrincipal = WebUI.getText(findTestObject('Object Repository/InsidePage/div_InsideRemainingPrincipal_MainL'))
						def secondaryRP = WebUI.getText(findTestObject('Object Repository/InsidePage/div_InsideRemainingPrincipal_Secondary'))
						def joinedRemainingPrincipal = mainRemainingPrincipal + secondaryRP
						myListRemainingPrincipal << groupsFromDB[i] + ": " + joinedRemainingPrincipal
						WebUI.delay(1)
						WebUI.click(findTestObject('Object Repository/InsidePage/div_Back to Page'))
						WebUI.delay(4)
						consumerElements = DriverFactory.getWebDriver().findElements(By.xpath("//div[@class='sc-bdVaJa sc-iwsKbI sc-fQkuQJ vSwtM']"))
						consumerElements = consumerElements.sort()
					}
					return myListRemainingPrincipal
				}
}

And this is my page:
image

When I click an object, I get some field text name and after that I come back on this page and trying to click next element.
Whats my problem. From Database I get sorted names like : Consumer, Consumer, Craft, Overdue. I want to sort this objects and click sorted objects, because I am printing elements with taken db name + name which i took from page: groupsFromDB[i] + ": " + joinedRemainingPrincipal.

But, the problem is, it clicks with the following iteration: Consumer, Craft, Consumer, Overdue.
I’ve also tried to write: consumerElements.sort(), but it does not work.

This is tricky, because calling any sort of sort() function on a list of WebElements doesn’t make any sense. With which criterion should it compare one WebElement to another? There’s no immediate answer, other than their order of appearance in the DOM, which is the same thing as not sorting at all.

The only thing I can think of is to implement a custom Comparator and use the Collections.sort() function in Java. This would look something like:

List<WebElement> consumerElements = DriverFactory.getWebDriver().findElements(By.xpath("//div[@class='sc-bdVaJa sc-iwsKbI sc-fQkuQJ vSwtM']"))

Comparator<WebElement> compareByText = new Comparator<WebElement>() {
    @Override
    public int compare(WebElement e1, WebElement e2) {
        return e1.getText().compareTo(e2.getText());
    }
};

Collections.sort(consumerElements, compareByText)
1 Like

@Brandon_Hein Thank you, I was made script similar like you did. I will use your script. thanks !

1 Like