I have a page with a dynamic table that periodically updates with new data. What I am trying to do is to click on a button and reload the page let's say every 3 seconds until the element from that table appears. I know the xpath of the element I want to appear, but I just can't make it work using the FluentWait. I tried configuring as seen in the code below, but when I'm calling the method it keeps clicking on the button extremly fast disregarding the pollingEvery configuration and without giving the page enough time to fully reload itself. Code can be checked out below.
What I am not sure about is the return statement. I don't fully grasp what should it be if I only need to click on a button until that element appears.
What am I missing?
public void clickButtonUntilElementIsDisplayed(WebElement elementToBeClicked, String xPathOfElementToBeDisplayed){
FluentWait<WebDriver> wait = new FluentWait<>(getDriver());
wait.pollingEvery(Duration.ofSeconds(5));
wait.withTimeout(Duration.ofSeconds(200));
wait.ignoring(NoSuchElementException.class);
Function<WebDriver,WebElement> clickUntilAppearance = webDriver -> {
while(webDriver.findElements(By.xpath(xPathOfElementToBeDisplayed)).isEmpty())
click(elementToBeClicked);
return webDriver.findElement(By.xpath(xPathOfElementToBeDisplayed));
};
wait.until(clickUntilAppearance);
}
The class in which this method can be found extends Page
CodePudding user response:
The return type of the Functional Interface need to be changed. There are 2 cases in which the functional interface will keep looping.
- Value returned is null.
- It threw an Exception mentioned as part of ignoring
Don't use while. Use if condition. For every pollingEvery this Functional Interface would get executed.
Function<WebDriver,WebElement> clickUntilAppearance = webDriver -> {
List<WebElement> tableElements = webDriver.findElements(By.xpath(xPathOfElementToBeDisplayed));
if(tableElements.isEmpty()) {
click(elementToBeClicked);
return null;
} else {
return tableElements.get(0);
}
};
CodePudding user response:
WebDriverWait w = new WebDriverWait(driver, 5);// 5seconds w.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("element_location")));