上QQ阅读APP看书,第一时间看更新
Filtering element attributes
In the example code, we will filter a list of images that have an empty alt attribute defined. This is useful if you want to check the accessibility of images displayed on the page. As per the accessibility guidelines, all images should have the alt attribute defined. This is done by filtering images, by testing the getAttribute("alt") method; it returns an empty string, as shown in the following code:
@Test
public void imgAltTest() {
List<WebElement> images = driver.findElements(By.tagName("img"));
System.out.println("Total Images : " + images.size());
List<WebElement> imagesWithOutAlt = images.stream()
.filter(item -> item.getAttribute("alt") == "")
.collect(Collectors.toList());
System.out.println("Total images without alt attribute " + imagesWithOutAlt);
}
The filter() function will return the list of all image elements that have an empty alt attribute defined.