等待执行两项操作-selenium / java

时间:2018-08-07 08:33:05

标签: selenium selenium-webdriver webdriver ui-automation fluentwait

我正在尝试使用Fluent wait执行以下两个操作:

  1. 点击搜索按钮
  2. 检查元素的结果

现在我正在尝试以下代码,但似乎无法正常工作:

 public SendMailPage waitForSometime() throws Exception {

    FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(Duration.ofMinutes(2))
            .pollingEvery(Duration.ofSeconds(10))
            .ignoring(NoSuchElementException.class);

    WebElement element = wait.until(new Function<WebDriver, WebElement>() {

        public WebElement apply(WebDriver driver) {
            driver.findElement(By.xpath("//BUTTON[@type='submit'][text()='Search']")).click();

            driver.findElement(By.xpath("xpath of the element i'm waiting to find"));

            return driver.findElement(By.xpath("xpath of the element i'm waiting to find"));
        }

    });

    element.isDisplayed();

    return new SendMailPage();
}

有人可以指导我如何解决此问题吗?

***更新的代码:在等待单个元素的地方也不起作用:

public SendMailPage assertMailSubject() throws Exception {
    FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(Duration.ofMinutes(2))
            .pollingEvery(Duration.ofSeconds(30))
            .ignoring(NoSuchElementException.class);

    WebElement element = wait.until(new Function<WebDriver, WebElement>() {

          public  WebElement apply(WebDriver driver) {


         return driver.findElement(By.xpath("the element that i am waiting for"));
                                        }
                                    }
    );
    return new SendMailPage();
}

1 个答案:

答案 0 :(得分:0)

我解决了两个问题:

  1. 由于NoSuchElementException来自Java util而非Selenium,因此代码无法正常工作。

  2. 为了执行两个操作,我只是在return语句之前通过Search键添加了操作。