我正在使用 Selenium Webdriver,我想在每个步骤后打印一些消息,这样我就可以在成功时打印一些消息,但在失败时我会收到“无法定位元素”。请看我的代码:
WebElement a= driver.findElement(By.xpath(".//*[@id='eviceSliderbuttonPrev']/a"));
if(a.isDisplayed())
{
System.out.println("Device Slider button exists");
a.click();
System.out.println("Button is clickable");
}
else {
System.out.println("Device Slider button doesn't exist!");
但是当案例失败并且我得到“无法定位元素”时,不会打印出 else 条件。
你知道如何解决这个问题吗?
最佳答案
当元素未显示时,您可以使用将返回列表的 driver.findElements(...),而不是抛出 NoSuchElementException 而不是返回 false 的 isDisplayed()。只需检查列表的大小为 0 或 1,而不是处理异常。
关于java - 如何在 Selenium webdriver 中将 "unable to locate element"更改为另一条消息,如 "Element doesn' t exist"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39013223/