java - Selenium 不等待页面加载

标签 java selenium selenium-webdriver

我正在用 Selenium 做一些测试,测试必须登录到系统。此登录需要 17 秒才能完全完成。 系统必须等待它完成,否则整个测试将失败。

我尝试了很多方法来做到这一点,但都失败了。

我试过的第一个代码是这样的:

driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);

当我使用它时,即使我告诉它等待 100 秒(这几乎是整整 2 分钟!),我也会在 2 秒后通过此堆栈跟踪获得超时。

org.openqa.selenium.WebDriverException: timeouts
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'CMTCLX62137', ip: '53.19.227.206', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_31'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\ALEX\AppData\Local\Temp\rust_mozprofile.Z2KJE568nWB8, rotatable=false, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, pageLoadStrategy=normal, moz:headless=false, platform=ANY, proxy=Proxy(manual, http=localhost), specificationLevel=0.0, moz:accessibilityChecks=false, acceptInsecureCerts=true, browserVersion=56.0, platformVersion=6.1, moz:processID=21116.0, browserName=firefox, javascriptEnabled=true, platformName=windows_nt}]
Session ID: b2dca4a5-623a-4311-ad07-6444785dbcaf
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:150)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:115)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:45)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
    at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions$RemoteTimeouts.implicitlyWait(RemoteWebDriver.java:868)

我试过的另一个代码:

new WebDriverWait(driver, 100).until(webDriver -> ((JavascriptExecutor) webDriver).executeScript(
        "return document.readyState").equals("complete"));

使用这个,它不会等待,我得到一个

org.openqa.selenium.NoSuchElementException: Unable to locate element

我的测试工作的唯一方法是使用 Thread.sleep(),但这是一个非常糟糕的选择,因为有时加载速度比预期快,有时它仍然失败,因为它需要超过 17 秒。

还有其他等待页面完全加载的选项吗?

最佳答案

这里已经解决了:Selenium wait until document is ready

无论如何,我通常会等待我需要使用的控件,而不是等到整个页面加载完毕:

wait.until(ExpectedConditions.elementToBeClickable(By
            .id(ConfigData.IDs.buttonLogin)));

关于java - Selenium 不等待页面加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46643265/

相关文章:

javascript - 为什么我的测试没有加载页面对象文件? (守夜人.js)

c# - 如何使用 Selenium、C# 从 dom 元素获取所有 css 样式

javascript - Python网页抓取选择下拉菜单

java - WebDriver 的 executeScript 返回警报文本而不是从 JavaScript 方法返回的值

java - 如何为对象链创建 flatMap 链?

java - com.twitter.sdk.android.core.TwitterApiException : HTTP request failed, 状态:403

java - 无法在方法之后打印 TestNg 中的语句

java - isDisplayed() 方法对于某些元素始终返回 true

java表达式查找多个单词

java - JUnit 测试、Hibernate 和未知实体错误