【问题标题】:Webdriver Expected condition failed: waiting for element to no longer be visiblrWebdriver 预期条件失败:等待元素不再可见
【发布时间】:2017-10-17 05:10:41
【问题描述】:

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

预期条件失败:等待元素不再存在 可见:By.cssSelector:.modal-body(尝试 6 秒,500 毫秒间隔) 构建信息:版本:'3.4.0',修订:'未知',时间:'未知' 系统信息:主机:'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' 驱动程序信息:org.openqa.selenium.chrome.ChromeDriver 功能 [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, 铬={铬驱动程序版本=2.29.461591 (62ebf098771772160f391d75e589dc567915b233), userDataDir=C:\Users\GI\AppData\Local\Temp\2\scoped_dir7780_13017}, 需要HeapSnapshot=true,pageLoadStrategy=正常, databaseEnabled=false,handlesAlerts=true,hasTouchScreen=false, 版本=58.0.3029.110,平台=XP,browserConnectionEnabled=false, nativeEvents=true,acceptSslCerts=true,locationContextEnabled=true, webStorageEnabled=true,browserName=chrome,takeScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, 意外警报行为=}] 会话 ID:eb353964f7b9bd515e527a795a111bc3

我的方法:

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

【问题讨论】:

  • 根据堆栈跟踪,我怀疑您正在使用等待接口的 FluentWait 实现。我建议您查看您的定位器".modal-body"。您的定位器可能会指向页面中的另一个元素,而您没有等待正确的元素不可见。

标签: java selenium selenium-webdriver webdriver


【解决方案1】:

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

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

所以可能你的元素确实是可见的。尝试增加您的超时时间,并可能在无法查看真实数据并知道元素是否可见时制作屏幕截图。

【讨论】:

    【解决方案2】:

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

    试试下面:

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

    【讨论】:

    • 要添加到这个答案中,原始发布者的版本会尝试从它所在的任何类(this.wait....)中执行等待语句,如 mohamed 的示例所示,您需要定义您从中检查元素可见性的驱动程序。我不知道为什么原始海报的代码,如图所示,甚至会编译。
    猜你喜欢
    • 2018-09-18
    • 2019-04-20
    • 2019-08-03
    • 2019-11-25
    • 2018-11-23
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多