gpt4 book ai didi

java - Selenium 有条件等待和中间操作

转载 作者:行者123 更新时间:2023-11-30 07:15:29 26 4
gpt4 key购买 nike

我正在尝试在 Selenium 中执行一项操作,它将等待状态更改为某个“已完成”状态。

从概念上讲,它可以用伪代码表示,如下所示:

public boolean waitForActionToComplete(long maxTimeoutInSeconds, int repeatTimeInSeconds, Callable<T> action, Callable<T> condition) {
long startTime = 0;
while (startTime < maxTimeoutInSeconds)
perform <action>; // e.g., click on a "Refresh" button to refresh the results
boolean done = verify <condition>; // e.g., check whether the job status is "Done"
if (done)
return true; // if done, then exit with TRUE
else
Thread.sleep(repeatTimeInSeconds);
end while;

return false; // status still not complete, timeout gracefully
}

此方法可能可以通过 ExpectedCondition 和 WebdriverWait/FluentWait 以某种简单的方式实现。但是,由于框架的某些限制,我无法完全像这样实现和使用该方法。上面的方法必须这样实现(在具有此方法签名的框架中实现接口(interface)):

public void execute(final WebDriver webDriver, String... parameters) {
// implementation here
}

谁能告诉我如何将方法转换为上面指定的形式?

最佳答案

我正在尝试在 Selenium 中执行一项操作,它将等待状态更改为某个“已完成”状态。

只需使用 WebDriverWaitExpectedConditions.textToBePresentInElementLocated 即可实现此目的,而不是创建自己的自定义方法 waitForActionToComplete,如下所示:-

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.textToBePresentInElementLocated(byObject, "Finished"));

希望有帮助...:)

关于java - Selenium 有条件等待和中间操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38488055/

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