【问题标题】:Selenium WebDriver (JAVA) - Fluent wait seems not to be working HALPSelenium WebDriver (JAVA) - 流畅的等待似乎不起作用
【发布时间】:2019-02-06 20:25:57
【问题描述】:

经过一段时间搜索我遇到的问题后,我无法找到任何解决方案。所以我来了。

一些背景知识,我正在尝试自动注册、确认和加入某个平台的“Live Class”。

为此,您每 10 分钟有一个 5 分钟的窗口,您可以在其中注册,然后确认,然后等待 X 时间,然后加入直播课程。

但这只是第一部分,我想注册。这就是我使用流利等待所做的:

public void joinPrivateClass() {

    System.out.println("Starting join private class");

             Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(Duration.ofSeconds(480))
            .pollingEvery(Duration.ofSeconds(5))
            .ignoring(NoSuchElementException.class);


    WebElement signUp = wait.until(new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            WebElement signUpButton = driver.findElement(By.xpath("//*[@id=\"live-class-322102\"]/div[1]/div/div/button"));

            if(signUpButton.isDisplayed()){
                System.out.println("button is displayed");
            }   else {
                System.out.println("button is not displayed yet");
            }
            return signUpButton;
        }
    }); signUp.click();


}

我的问题是,在页面加载后,虽然 fluentwait “工作”,但我希望每 5 秒有一个“按钮不显示”,直到它被点击。但是无论是否显示按钮,我都没有收到任何消息,“按钮未显示”和“按钮已显示”,所以我认为“wait.until”中出现了问题

有些事情要提一下,我不是程序员,如果我做错了很抱歉,

同样在我的 IDE (intelliJ) 中,它标志着我是这部分代码的“驱动程序”

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)

在“紫色”和这个“司机”中:

public WebElement apply(WebDriver driver)

如果与此有关,则为灰色!感谢您的宝贵帮助

【问题讨论】:

    标签: java selenium webdriver wait fluentwait


    【解决方案1】:

    您的wait 是用ignoring(NoSuchElementException.class) 构建的。因此,只要该按钮不存在,您的 wait.until(...) 就会默默地在 findElement(...) 行继续失败 - 它永远不会到达 println(...)

    从您的等待中删除 ignoring(...),并将您的 wait.until(...) 正文更改为:

    WebElement signUpButton;
    try {
        signUpButton = driver.findElement(By.xpath("//*[@id=\"live-class-322102\"]/div[1]/div/div/button"));
        System.out.println("button is displayed");
    } catch(NoSuchElementException ignored) {
        System.out.println("button is not displayed yet");
    }
    return signUpButton;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 2017-04-19
      • 1970-01-01
      • 1970-01-01
      • 2019-01-03
      • 2015-10-09
      • 2023-03-05
      相关资源
      最近更新 更多