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:

@Testpublic 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 ...

Get Selenium WebDriver 3 Practical Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.