限制要存储在列表中的元素<;WebElement>;-selenium/java

限制要存储在列表中的元素<;WebElement>;-selenium/java,selenium,selenium-webdriver,ui-automation,Selenium,Selenium Webdriver,Ui Automation,我需要知道如何在列表中存储前10个值,而不是存储所有存在的元素 现在,我的代码存储所有元素: By mySelector = By.xpath("/html/body/div[1]/div/section/div/div[2]/form[1]/div/ul/li"); List<WebElement> myElements = driver.findElements(mySelector); for(WebElement e : myElements) { System.out.

我需要知道如何在
列表中存储前10个值,而不是存储所有存在的元素

现在,我的代码存储所有元素:

By mySelector = By.xpath("/html/body/div[1]/div/section/div/div[2]/form[1]/div/ul/li");
List<WebElement> myElements = driver.findElements(mySelector);
for(WebElement e : myElements) {
  System.out.println(e.getText());
}
By mySelector=By.xpath(“/html/body/div[1]/div/section/div/div[2]/form[1]/div/ul/li”);
List myElements=driver.findElements(mySelector);
for(WebElement e:myElements){
System.out.println(e.getText());
}
尝试将
position()
与li一起使用

By.xpath("/html/body/div[1]/div/section/div/div[2]/form[1]/div/ul/li[position() < 11]")
By.xpath(“/html/body/div[1]/div/section/div/div[2]/form[1]/div/ul/li[position()<11]”)
尝试将
position()
与li一起使用

By.xpath("/html/body/div[1]/div/section/div/div[2]/form[1]/div/ul/li[position() < 11]")
By.xpath(“/html/body/div[1]/div/section/div/div[2]/form[1]/div/ul/li[position()<11]”)