本文介绍了Selenium中的try catch块中的带有isDisplayed()方法的NoSuchElementException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查阴性情况.上面的布尔元素未显示,但我必须打印true和false,但没有显示此类元素异常请帮忙.

i want to check negative condition.above boolean element is not displayed ,but i have to print true and false but it shows no such element exceptionplease help.

try{

    boolean k= driver.findElement(By.xpath("xpath_of_element")).isDisplayed();
    if(!k==true)
    {
             System.out.println("true12"); 
    }

}catch (NoSuchElementException e) {
    System.out.println(e);
}

推荐答案

元素有两个不同的阶段,如下所示:

There are two distinct stages of an element as follows:

  • Element present within the HTML DOM
  • Element visible i.e. displayed within the DOM Tree

正如您所看到的 NoSuchElementException 实质上表明,元素在 视口中没有存在 ,并且在所有可能的情况下 isDisplayed() 方法将返回 false .因此,要验证这两个条件,可以使用以下解决方案:

As you are seeing NoSuchElementException which essentially indicates that the element is not present within the Viewport and in all possible conditions isDisplayed() method will return false. So to validate both the conditions you can use the following solution:

try{
    if(driver.findElement(By.xpath("xpath_of_the_desired_element")).isDisplayed())
        System.out.println("Element is present and displayed");
    else
        System.out.println("Element is present but not displayed"); 
}catch (NoSuchElementException e) {
    System.out.println("Element is not present, hence not displayed as well");
}

这篇关于Selenium中的try catch块中的带有isDisplayed()方法的NoSuchElementException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 12:52