Getting error "type of expression must be an array type but it is resolved to Llist<WebElement>"

Viewed 44

Using Selenium Web driver in Java - trying to get all the values from a dropdown menu and storing to a list then checking if the given value is present in the list. If it is present then return true else false. I am new to selenium and learning things slowly kindly help me to resolve this issue. Thanks


 public void verifyResults() {
Boolean found=false;
    WebElement ele=driver.findElement(By.xpath("/html/body/div[1]/div/form/div[3]/div[1]/div[1]/div[2]/div/div[1]/div/div[2]/ul")); 
 Select sel = new Select(ele);
List<WebElement> el = sel.getOptions();
for(int i=0;i<el.size();i++){
     if(el[i].Equals("Agra")) {
          found=true;
break;
}
}
if(found){
     System.out.println(ele.getText());
}


   }
1 Answers

Lists in java don't have the [] operator, only arrays. Instead, you can use the get method.

Additionally, there are a few other problems in the snippet you shared. First, note that it's equals, not Equals. Second, the elements in el are WebElements, not strings, so an element will never be equal to "Agra". I assume you meant to compare it with the text of the element:

for (int i = 0; i < el.size(); i++){
     if (el.get(i).getText().equals("Agra")) {
          found = true;
          break;
     }
}

As a side note, it's worth noting that using streams could shorten the code considerably:

boolean found = el.stream().anyMatch(e -> e.getText().equals("Agra"));
Related