Selenium findElement-s 不遵循显式等待,而仅遵循隐式等待

问题描述 投票:0回答:1

我想使用显式等待而不是 20 秒(隐式等待)使以下 junit + selenium-jvm-test 在 3 秒内失败,而无需摆弄隐式等待设置。我怎样才能实现这个目标?

不幸的是

findElements
也等待了整整20秒,以及所有依赖于
ExpectedConditions
findElements
findElement
。你有什么想法吗?

    @Test
    void isGoogleOpen() {
        // this.driver.manage().timeouts().implicitlyWait(Duration.of(20, ChronoUnit.SECONDS)); happens in @BeforeEach
        this.driver.get("https://google.de");

        String wrongClassName = "lnXdpdIAMERROR";

        WebDriverWait wait = new WebDriverWait(this.driver, Duration.ofSeconds(3));
        wait.until(d -> d.findElement(By.className(wrongClassName)).isDisplayed());
    }
java selenium-webdriver junit jvm
1个回答
0
投票

你可以尝试重写这段代码,这样如果该元素在3秒内没有出现,就会抛出你需要的异常。这是一个可能有帮助的代码:

@Test

void isGoogleOpen() { this.driver.get("https://google.de");

String wrongClassName = "lnXdpdIAMERROR";

WebDriverWait wait = new WebDriverWait(this.driver,Duration.ofSeconds(3));
try {  wait.until(ExpectedConditions.visibilityOfElementLocated(By.className(wrongClassName)));
} catch (TimeoutException e) {
    // Case processing, коли елемент не з'являється протягом 3 секунд
    // Наприклад, виведення повідомлення про невдачу тесту
    System.out.println("Елемент не знайдений протягом 3 секунд");
}

}

如果有帮助的话请尝试一下并给我反馈

© www.soinside.com 2019 - 2024. All rights reserved.