gpt4 book ai didi

image - 无法列出 Selenium 中缺少标题属性的图像

转载 作者:行者123 更新时间:2023-11-28 21:36:21 26 4
gpt4 key购买 nike

下面是我的代码,我试图获取缺少 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)]"));

关于image - 无法列出 Selenium 中缺少标题属性的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58410275/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com