问题
I'm trying to use FluentWait instead of sleep and this is my first practice. First of all and most importantly am I doing it right at all? Secondly I got through two elements so I thought it kind of works (PaymentMethod button and CheckOut button). Before I implemented FluentWait it wouldn't find them. And finally it won't find the third(backToDesktop button) element. Keeps throwing Element not visible, although I added the wait.ignore(ElementNotVisibleExcecption.class).
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(login.getDriver());
wait.withTimeout(5, TimeUnit.SECONDS);
wait.pollingEvery(250, TimeUnit.MILLISECONDS);
wait.ignoring(NoSuchElementException.class);
wait.ignoring(ElementNotVisibleException.class);
WebElement paymentMethod = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return login.getDriver().findElement(By.xpath("//*[@id='paymentMethodHolder']/div[1]/div[1]/button"));
}
});
paymentMethod.click();
System.out.println("FOUND PAYMENTMETHOD BUTTON");
WebElement checkOut = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return login.getDriver().findElement(By.xpath("//*[@id='checout-footer-buttons']/button[2]"));
}
});
checkOut .click();
System.out.println("FOUND KINNITA BUTTON");
WebElement backToDesktop= wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return login.getDriver().findElement(By.className("modal-button-text"));
}
});
backToDesktop.click();
System.out.println("FOUND BACKTODESKTOP BUTTON");
回答1:
FluentWait is a custom wait. You shouldn't need it in most cases. You should always start with a WebDriverWait and ExpectedConditions and if that doesn't work, then maybe investigate a FluentWait. My guess is that something simple like the below will work for you. This is just one example. You should look at all the different conditions you can wait for that are provided by ExpectedConditions. Probably the most common ones are waiting for an element to be visible or clickable.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("someId")));
来源:https://stackoverflow.com/questions/42952428/selenium-fluentwait-and-element-not-visible-exception