Wait to perform two actions - selenium/ java

787 Views Asked by At

I'm trying to use Fluent wait to perform two actions as below:

  1. Click on search button
  2. Check the result for the element

Right now I'm trying with the below code and it doesn't seem to work:

 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();
}

Can someone guide me on how to fix this?

***UPDATED CODE: where waiting for a single element also doesn't work :

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

There are 1 best solutions below

0
On BEST ANSWER

I fixed both the problems:

  1. The code was not working as the NoSuchElementException was from Java util instead of Selenium.

  2. And for performing two actions, I just added by Search key action before the return statement.