简体   繁体   English

Selenium FluentWait和元素不可见异常

[英]Selenium FluentWait and element not visible exception

I'm trying to use FluentWait instead of sleep and this is my first practice. 我正在尝试使用FluentWait代替睡眠,这是我的第一次练习。 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). 其次,我了解了两个要素,因此我认为它很有效(PaymentMethod按钮和CheckOut按钮)。 Before I implemented FluentWait it wouldn't find them. 在实施FluentWait之前,找不到它们。 And finally it won't find the third(backToDesktop button) element. 最后,它将找不到第三个(backToDesktop按钮)元素。 Keeps throwing Element not visible, although I added the wait.ignore(ElementNotVisibleExcecption.class). 尽管我添加了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 is a custom wait. FluentWait是一个自定义等待。 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 . 您应该始终从WebDriverWaitExpectedConditions ,如果这不起作用,则可以研究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 . 您应该查看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")));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM