硒等待方法返回true

gsi*_*dze 0 c# selenium selenium-webdriver

我知道 selenium webdriver 可以做到这一点:

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.ClassName("someclass")));
Run Code Online (Sandbox Code Playgroud)

我不能用我的方法做到这一点吗?例如,我有一种方法可以截取屏幕截图并与另一张图片进行比较。我想等到该方法返回 true。

所以我有这个代码

while (WelcomeScreen(driver) != true)
{
    Thread.Sleep(1000);   
}
Run Code Online (Sandbox Code Playgroud)

我就不能找到更好的解决办法吗?

Gau*_*hah 5

你可以使用 FluentWait 我不擅长 C#,所以下面的代码示例是 Java 的。如果你能将它转换为 C#,我认为它可能会起作用。

 Wait wait = new FluentWait<WebDriver>(driver)
        .withTimeout(30, TimeUnit.SECONDS)
        .pollingEvery(5, TimeUnit.SECONDS)
        .ignoring(NoSuchElementException.class);

 wait.until(new Function<WebDriver, Boolean>() {
                public Boolean apply(WebDriver driver) {
                    return WelcomeScreen(driver)
                }
              }
 );
Run Code Online (Sandbox Code Playgroud)