硒流利等待找不到元素

时间:2015-06-18 11:39:17

标签: java selenium

我有一个执行多个ajax请求的页面。当所有请求都返回后,一个名为loading-complete的类被添加到body元素中。在selenium中,我尝试等待这个类存在,然后我开始寻找要验证的元素。这是方法:

public static final long DEFAULT_POLL_INTERVAL = 1L;
public static final long DEFAULT_TIMEOUT = 60L;

public static void waitForLoadingComplete(WebDriver driver) {
    Wait<WebDriver> wait = IkWaitUtil.createFluentWait(driver, DEFAULT_TIMEOUT, DEFAULT_POLL_INTERVAL);
    wait.until(new Function<WebDriver, WebElement>() {
        @Override
        public WebElement apply(WebDriver arg0) {
            return arg0.findElement(By.cssSelector("body.loading-complete"));
        }
    });
}

这看起来随机失败,出现以下错误和堆栈跟踪:

org.openqa.selenium.TimeoutException: Timed out after 60 seconds waiting for mypackage.util.MyUtil$10@17667bd
Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:03:00'
System info: host: 'my_host', ip: 'a.b.c.d', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_24'
Driver info: driver.version: unknown
    at org.openqa.selenium.support.ui.FluentWait.timeoutException(FluentWait.java:259)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:228)
    at mypackage.util.MyUtil.waitForLoadingComplete(MyUtil.java:287)
    at mypackage.util.MyUtil.getDisplayedWebElement(MyUtil.java:217)
    at mypackage.util.MyUtil.getDisplayedWebElement(MyUtil.java:212)
    at mypackage.bdd.MyCommonSteps.openAndSearch(MyCommonSteps.java:116)
    at mypackage.bdd.MyCommonSteps.givenSearchRequisites(MyCommonSteps.java:31)
    at mypackage.bdd.subpackage.StatusSteps.givenTheUserSearches(StatusSteps.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.jbehave.core.steps.StepCreator$ParameterizedStep.perform(StepCreator.java:428)
    at org.jbehave.core.embedder.StoryRunner$FineSoFar.run(StoryRunner.java:436)
    at org.jbehave.core.embedder.StoryRunner.runStepsWhileKeepingState(StoryRunner.java:419)
    at org.jbehave.core.embedder.StoryRunner.runScenarioSteps(StoryRunner.java:384)
    at org.jbehave.core.embedder.StoryRunner.runParametrisedScenariosByExamples(StoryRunner.java:363)
    at org.jbehave.core.embedder.StoryRunner.runIt(StoryRunner.java:256)
    at org.jbehave.core.embedder.StoryRunner.run(StoryRunner.java:181)
    at org.jbehave.core.embedder.StoryRunner.run(StoryRunner.java:164)
    at org.jbehave.core.embedder.Embedder$EnqueuedStory.call(Embedder.java:620)
    at org.jbehave.core.embedder.Embedder$EnqueuedStory.call(Embedder.java:584)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == body.loading-complete (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 10.20 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:03:00'
System info: host: 'my_host', ip: 'a.b.c.d', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_24'
Session ID: 0f5db388-ce4b-4310-ad4a-edc452bf5f40
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0, enablePersistentHover=true, ignoreZoomSetting=false, ie.ensureCleanSession=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss, version=11, ie.usePerProcessProxy=false, cssSelectorsEnabled=true, ignoreProtectedModeSettings=false, requireWindowFocus=false, initialBrowserUrl=http://localhost:17512/, handlesAlerts=true, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, ie.browserCommandLineSwitches=, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:441)
    at org.openqa.selenium.By$ByCssSelector.findElement(By.java:426)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
    at mypackage.util.MyUtil$10.apply(MyUtil.java:290)
    at mypackage.util.MyUtil$10.apply(MyUtil.java:287)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:208)
    ... 25 more

所以,既然我的方法有时候工作,有时也没有,并且失败对我来说似乎是随机的(手动测试显示类已设置),我想知道我是否正在实现这个错误。更短的超时和多次重试是否更好,或者是否有某种方法可以使行为稳定且可重复?

2 个答案:

答案 0 :(得分:1)

你有一个加载指示器。对于明确的等待来说,这甚至更好。只需稍微说明显式等待,默认情况下它会休眠500毫秒并发送请求以查看元素是否存在,直到达到超时。在这种情况下,我认为这是最合适的。

inline int VxSubfile::IndexOfFieldInSchema(char * columnName)
{
    std::map<char const*, int, cmp_str>::iterator iter = _fieldLookup.find(columnName);
    if(iter == _fieldLookup.end())
    {
        return -1;
    }
    else{
        jdebug("Returning index : %d", iter->second);
        return iter->second;
    }
}

这个等待应该等到10s并且每500ms检查一次元素的存在

答案 1 :(得分:0)

对于你的直接问题,我强烈建议增加超时。根据堆栈跟踪,这是问题所在:

  

org.openqa.selenium.TimeoutException:60秒后超时   等待

至于明确的等待,这应该有所帮助:

new WebDriverWait(driver, 600000L, 2000L).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("body.loading-complete")));

等待最多10分钟,每2秒轮询一次。