gpt4 book ai didi

java - Do While 循环和 ExpectedConditions - 可以一起使用吗?

转载 作者:行者123 更新时间:2023-12-01 08:49:17 25 4
gpt4 key购买 nike

Do While 循环和 ExpectedConditions - 可以一起使用吗?

  1. 我使用下面列出的方法遇到了奇怪的无法定位元素/超时异常。

<强>2。是否可以添加一个 while 循环,以便该方法可以多次检查和执行?

    public void waitAndClickElement(WebElement element) throws InterruptedException {
try {
this.wait.until(ExpectedConditions.elementToBeClickable(element)).click();
System.out.println("Successfully clicked on the WebElement: " + "<" + element.toString() + ">");
}catch (Exception e) {
System.out.println("Unable to wait and click on WebElement, Exception: " + e.getMessage());
Assert.assertFalse(true, "Unable to wait and click on the WebElement, using locator: " + "<" + element.toString() + ">");
}
}

最佳答案

是的,这是可能的。像这样的事情:

    boolean clicked = false;
int attempts = 0;
while(!clicked && attempts < 5) {
try {
this.wait.until(ExpectedConditions.elementToBeClickable(element)).click();
System.out.println("Successfully clicked on the WebElement: " + "<" + element.toString() + ">");
clicked = true;
} catch (Exception e) {
System.out.println("Unable to wait and click on WebElement, Exception: " + e.getMessage());
Assert.assertFalse(true, "Unable to wait and click on the WebElement, using locator: " + "<" + element.toString() + ">");
}
attempts++;
}

在 while 循环中添加最大尝试次数可能是个好主意,这样您就不会陷入无限循环。

关于java - Do While 循环和 ExpectedConditions - 可以一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42483776/

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