-2

Currently i'm able to print all of them but i want to find out the largest and print only that value.

Can anyone please help me in building the code for this in Selenium Webdriver & Java.

Here is my code:

List <WebElement> Rating  =  oBrowser.findElements(By.xpath("//span[contains(@class, 'rating-out-of-five')]");
try{
  for(WebElement starRating:Rating)
   {
     System.out.println(starRating.getText());
   }
 catch(Exception e){
   System.out.println("Rating not found");
 }
Ethaan
  • 11,291
  • 5
  • 35
  • 45

1 Answers1

0

Before your loop, introduce the variable that will hold the max value like so;

int max = Integer.parseInt(Rating.get(0).getText());

Note it is wrong to do something like int max = 0; (consider a list of negative values).

Then, in your for loop you can check if the current value is greater than the max value like so;

int tmp = Integer.parseInt(starRating.getText());
if (tmp > max) max = tmp;

Sure you could make an oneliner out of it, but this is more readable.

Now, after your loop, max will hold the greatest value of your list, so you can simply print

System.out.println(max);

By the way, I don't know the methods of your WebElement class, but if it has a method such as getValue which returns an int it would make things simpler.

Jyr
  • 711
  • 5
  • 16