有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Webdriver预期条件失败:等待元素不再可见BLR

我有一个方法,等待css(模态)定位器在屏幕上不可见,在我的一些构建中,我得到以下失败消息

Expected condition failed: waiting for element to no longer be visible: By.cssSelector: .modal-body (tried for 6 second(s) with 500 MILLISECONDS interval) Build info: version: '3.4.0', revision: 'unknown', time: 'unknown' System info: host: 'DEV007', ip: '172.16.2.192', os.name: 'Windows Server 2008 R2', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_131' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461591 (62ebf098771772160f391d75e589dc567915b233), userDataDir=C:\Users\GI\AppData\Local\Temp\2\scoped_dir7780_13017}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=58.0.3029.110, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}] Session ID: eb353964f7b9bd515e527a795a111bc3

我的方法是:

public boolean waitUntilModalDisapears() {
    return this.wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".modal-body")));
}

共 (2) 个答案

  1. # 1 楼答案

    这种方法没有错,你用对了。 这种方法(在C#上)的代码是:

    return (Func<IWebDriver, bool>) (driver =>
    {
    try
    {
      return !driver.FindElement(locator).Displayed;
    }
    catch (NoSuchElementException ex)
    {
      return true;
    }
    catch (StaleElementReferenceException ex)
    {
      return true;
    }
    });
    

    所以可能你的元素确实是可见的。试着增加你的超时时间,或者在无法查看真实数据的情况下截图,并知道元素是否可见

  2. # 2 楼答案

    每次运行代码时,在页面中加载web元素都会有所不同,因此您应该增加web驱动程序等待的时间,并尝试多次运行代码,以确保驱动程序已经等待了足够的时间来加载元素

    请尝试以下内容:

     WebDriverWait wait = new WebDriverWait(driver, 40);
     wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".modal-body")));