无法列出硒中缺少标题属性的图像

Not able to list images whose title attribute is missing in selenium

下面是我的代码,我试图获取缺少 title 属性但无法正常工作的图像的 src。使用下面的代码显示所有图像但它不搜索没有标题的图像

public void checkimagetitle() {

    suites.setupEnviroment();
    WebDriver driver = suites.getWebDriver();
    driver.get(suites.WEB_PATH);
    Dimension d = new Dimension(1455, 900);
    driver.manage().window().setSize(d);

    try {
        List<WebElement> listImages = driver.findElements(By.tagName("img"));

        System.out.println("No. of Images: " + listImages.size());

        int counter = 0;
        for (WebElement image : listImages) {
            boolean title = image.getAttribute("title") != null;

            if (title = false) {
                counter++;
                System.out.println(image.getAttribute("src"));
            } else {
                System.out.println("failed");

            }
        }
        System.out.println("No. of total images without title: " + counter);

        Logger.getLogger("results").log(new LogRecord(Level.INFO,
                MethodHandles.lookup().lookupClass().getCanonicalName() != null ? "success" : "failure"));

    } catch (Exception e) {
        Logger.getLogger("results").log(new LogRecord(Level.INFO,
                MethodHandles.lookup().lookupClass().getCanonicalName() == null ? "success" : "failure"));
        System.out.println(e);
    }
    driver.close();

}

提前致谢!

找到更简单的解决方案。您可以使用 XPATH 直接检测没有 title 属性的 img 元素,而不是遍历所有图像

//img[not(@title)]
List<WebElement> listImageswithoutTitle = driver.findElements(By.xpath("//img[not(@title)]"));