Selenium FluentWait and element not visible exception

醉酒当歌 提交于 2020-01-06 17:59:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!