Webdriver FluentWait不忽略异常 [英] Webdriver FluentWait not ignoring exceptions

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

问题描述

我已经使一些通过此页面的用户流程自动化 http://www.efinancialcareers.co.uk/search . 当我使用左侧精简栏缩小搜索范围时,会出现一个叠加层,用户必须等待,直到返回搜索结果.该方法等待叠加层出现,然后等待其消失.

I have automated some user flows which go via this page http://www.efinancialcareers.co.uk/search. When I narrow a search using the left hand-side refine rail, an overlay appears where a user has to wait until the search results are returned. The method waits for the overlay to appear and then waits for it to disappear.

public void waitForSRPOverlayToComplete() {

    Wait<WebDriver> wait = new FluentWait<WebDriver>(getDriver())
            .withTimeout(5, TimeUnit.SECONDS)
            .pollingEvery(1, TimeUnit.NANOSECONDS)
            .ignoring(NoSuchElementException.class)
            .ignoring(TimeoutException.class);

    **// Error occurs here**
    WebElement blockedOverlay = wait.until(new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            return driver.findElement(By.className("blockOverlay"));
        }
    });

    Wait<WebDriver> wait2 = new FluentWait<WebDriver>(getDriver())
            .withTimeout(5, TimeUnit.SECONDS)
            .pollingEvery(1, TimeUnit.NANOSECONDS)
            .ignoring(NoSuchElementException.class)
            .ignoring(TimeoutException.class);

    wait2.until(ExpectedConditions.stalenessOf(blockedOverlay));
}

有时会遇到超时异常,因为找不到元素(blockOverlay).发生这种情况时,我已经观察到该页面,并且确实出现了叠加层,但是我认为有时候,当搜索速度非常快时,上述方法会漏掉它. 我不明白为什么会遇到技术错误,因为我已经说了流利的等待,可以忽略它们.

On occasion I get a Timeout exception as the element (blockOverlay) is not found. I have observed the page when this occurs and the overlay does appear but I think sometimes when the search is very fast the above method misses it. I don’t understand why I get a technical error as I have told the fluent wait to ignore them.

这是使叠加层显示的代码:

This is the code to make the overlay appear:

$('body').block({
            message: $('#loaderEl'),
            css: {
                backgroundColor: 'transparent',
                border: "none",
                color: '#333333',
                fontWeight: 'bolder',
                top: ($(window).height() - $('#loaderEl').height()) / 2 + $(window).scrollTop() + "px"
            },
            overlayCSS: {
                backgroundColor: '#f8f8f8'
            },
            centerY: false
        });

并删除它

$('body').unblock();

这是我收到的错误:

Caused by: org.openqa.selenium.TimeoutException: Timed out after 5 seconds waiting for     com.efinancialcareers.myefc.desktop.BasePage$6@548238e0
Build info: version: '2.35.0', revision: '8df0c6bedf70ff9f22c647788f9fe9c8d22210e2',     time: '2013-08-17 12:46:41'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_26'
Driver info: driver.version: unknown
    ... 33 more
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"class name","selector":"blockOverlay"}

任何帮助或建议将不胜感激.

Any help or suggestions would be appreciated.

推荐答案

FluentWait实际上超时时,您不能取消TimeoutException.这就是API的本质.如果这是您真正要忽略的异常,则需要捕获TimeoutException.

You can't suppress TimeoutException when the FluentWait actually times out. That's simply the nature of the API. If this is an exception you truly want to ignore, you need to catch the TimeoutException.

此外,作为一个旁注,尝试每纳秒轮询一次条件可能会适得其反.您应该将轮询间隔延长到每200或250毫秒一次.

Also, as a side note, attempting to poll for the condition every nanosecond is likely counterproductive. You should probably extend your polling interval to something like every 200 or 250 milliseconds.

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

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