Try this
[code:2i0osyin] package yahoo;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Yahoo {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://www.yahoo.com");
driver.findElement(By.xpath("//*[@id='UHSearchBox']")).sendKeys("Pizza");
List<WebElement> searchList=driver.findElements(By.xpath("//*[starts-with(@id,'yui')]/li/a"));
System.out.println("Total searched items are= "+searchList.size());//will count all the elements searched by pizza
System.out.println(searchList.get(4).getText());//will print 5th item which is pizza delivery
System.out.println("===============================");
//will print all the available items
for(int i=0;i<10;i++){
if(!(searchList.get(i).getText().isEmpty())){
System.out.println(searchList.get(i).getText());
}
}
}
}[/code:2i0osyin]
I think you need to put all the elements in a list and then access the index of it.