Selenium FluentWait 和元素不可见异常 [英] Selenium FluentWait and element not visible exception

查看:31
本文介绍了Selenium FluentWait 和元素不可见异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 FluentWait 而不是 sleep,这是我的第一次练习.首先,最重要的是我做对了吗?其次,我通过了两个元素,所以我认为它有点工作(PaymentMethod 按钮和 CheckOut 按钮).在我实施 FluentWait 之前,它找不到它们.最后它不会找到第三个(backToDesktop 按钮)元素.尽管我添加了 wait.ignore(ElementNotVisibleExcecption.class).

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");

推荐答案

FluentWait 是自定义等待.在大多数情况下,您不应该需要它.你应该总是从 WebDriverWaitExpectedConditions 开始,如果这不起作用,那么也许调查一个 FluentWait.我的猜测是像下面这样简单的东西对你有用.这只是一个例子.您应该查看 ExpectedConditions.可能最常见的是等待元素可见或可点击.

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")));

这篇关于Selenium FluentWait 和元素不可见异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆