gpt4 book ai didi

java - Selenium 流畅等待未找到元素

转载 作者:行者123 更新时间:2023-11-30 08:07:18 24 4
gpt4 key购买 nike

我有一个执行多个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

因此,由于我的方法有时有效,有时无效,并且失败对我来说似乎是随机的(手动测试显示该类已设置),我想知道我是否错误地实现了这一点。较短的超时和多次重试是否更好,或者是否有某种方法使行为稳定且可重复?

最佳答案

您有一个加载指示器。这对于显式等待来说甚至更好。简单介绍一下显式等待,它默认 hibernate 500 毫秒,并发送请求以查看元素是否存在,直到超时为止。我认为这是最适合这种情况的。

By css = By.cssSelector("body.loading-complete"));
(new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(css))

此等待应该最多等待 10 秒,并每 500 毫秒检查一次元素是否存在

关于java - Selenium 流畅等待未找到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30914331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com