2014-03-25 84 views
22

在下面的代码,我试图等到一个元素可见:硒 - 等到元素不可见

var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10)); 
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("processing"))); 

是否可以告诉司机要等到该元素是不可见的?

回答

10

以下应等待,直到元件不再显示即不可见(或超时10秒后)

var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10)); 
wait.Until(driver => !driver.FindElement(By.Id("processing")).Displayed); 

它将引发异常,如果一个元件不能与ID processing找到。

+0

这就是问题所在,它抛出一个异常: – Tito

+0

它会抛出异常。 TDHM有正确答案。 –

26

是的,这可能与方法invisibilityOfElementLocated

wait.until(ExpectedConditions.invisibilityOfElementLocated(locator)); 
+11

在C#中看不到此选项? – Coops

+0

这是正确的 –

+2

有一件令人厌烦的事情是你需要等待隐式等待的持续时间。恼人的意外执行。 (Selenium 2.48.2) –

-13

在下面,其用于停止的驱动程序几秒钟

System.Threading.Thread.Sleep(20000)的码;

+4

永远不要在任何动态情况下使用Thread.Sleep()。没有任何最佳实践,但这尽可能接近最差实践。相反,请看本文中其他地方显示的动态条件WebDriverWait示例。 –

+0

你也可以手动测试....等待20秒??! –

0

我知道这是旧的,但是因为我正在寻找解决方案,所以我想我会添加我的想法。

上面给出如果设置了IgnoreExceptionTypes属性应该工作答案:

var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10)); 
wait.IgnoreExceptionTypes = new[] { typeof(NoSuchElementException) } 
wait Until(driver => !driver.FindElement(By.Id("processing")).Displayed); 
0
public void WaitForElementNotVisible(string id, int seconds) 
    { 

     try 
     { 
      var wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(seconds));     
      wait.Until(driver1 => !visibility(id)); 
      Console.WriteLine("Element is not visible.."); 
     } 
     catch (WebDriverTimeoutException) 
     { 
      Assert.Fail("Element is still visible.."); 
     } 


    } 


    bool visibility(string id) 
    { 
     bool flag; 
     try 
     { 
      flag = driver.FindElement(By.Id(locator)).Displayed; 
     } 
     catch (NoSuchElementException) 
     { 
      flag = false; 
     } 
     return flag; 
    } 
+0

你可以添加一些上下文/解释吗? – Robert

+1

例如,您的应用程序在处理请求或下载文档时有确实的加载覆盖。您希望等到覆盖消失。您可以使用上述方法。参数“id”可用于提供Web元素名称和“秒”来设置脚本在将测试标记为失败之前应等待的时间。 – Anoop

1

使用隐身法,这里有一个例子使用。

final public static boolean waitForElToBeRemove(WebDriver driver, final By by) { 
    try { 
     driver.manage().timeouts() 
       .implicitlyWait(0, TimeUnit.SECONDS); 

     WebDriverWait wait = new WebDriverWait(UITestBase.driver, 
       DEFAULT_TIMEOUT); 

     boolean present = wait 
       .ignoring(StaleElementReferenceException.class) 
       .ignoring(NoSuchElementException.class) 
       .until(ExpectedConditions.invisibilityOfElementLocated(by)); 

     return present; 
    } catch (Exception e) { 
     return false; 
    } finally { 
     driver.manage().timeouts() 
       .implicitlyWait(DEFAULT_TIMEOUT, TimeUnit.SECONDS); 
    } 
} 
+1

该文档说:“期望检查元素在DOM上是不可见还是不存在”。为什么你需要'.ignoring(NoSuchElementException.class)'? – SirLenz0rlot

+0

在这种情况下,它报告了我日志中不必要的错误数量。忽略该例外清理日志,但老实说,我切换到使用所有JavaScript来做这些检查。它更快,更干净。我发现这种硒检查有时候不起作用。 –

4
var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10)); 
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("processing"))); 

的想法是等到元素是不可见的。第一行设置元素必须消失的等待时间;这里是10秒。第二行使用硒来检查是否满足条件“隐藏ElementLocated”。元素在主题情况下通过其id找到,即id="processing"。如果元素在请求的时间段内没有消失,则会引发超时异常,并且测试将失败。

+0

你能否请[编辑]解释为什么这段代码回答这个问题?仅限代码答案[阻止](http://meta.stackexchange.com/q/148272/274165),因为他们没有教导解决方案。 –

+0

当然。我认为这是可以自我解释的,我的不好。 主题为:等到元素不可见。 第一行设置元素必须消失的等待时间,此处为10秒。 第二行使用硒来检查是否满足条件“invisibilityofElementLocated”。元素通过它的id在主题情况中找到,即id =“processsing” 如果元素在请求的时间段内不会消失,则会引发Timeout异常并且测试将失败。 –

2

是的,你可以创建你自己的ExpectedCondition,只需恢复可见到不可见

这里是如何做到这一点在Python:

from selenium.webdriver.support.expected_conditions import _element_if_visible 

class invisibility_of(object): 

    def __init__(self, element): 
     self.element = element 

    def __call__(self, ignored): 
     return not _element_if_visible(self.element) 

以及如何使用它:

wait = WebDriverWait(browser, 10) 
wait.until(invisibility_of(elem)) 
+0

适合python!看起来,c#是另一回事。这个概念依然存在,我知道... – benzkji