0

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 exception please 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);
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
user7309686
  • 69
  • 1
  • 2
  • 7
  • Because driver.findElement is not able to find any such xpath and thus k is not assigned any value .before that NoSuchElementException is printed . You can confirm this via removing try catch block ...it will print the same exception again . You need to work on the xpath here or for some reasons it's not visible on page . – Karan Jun 23 '18 at 16:55
  • I know xpath is not available because radio button not available .but my testcase is to check if it is displaying print false – user7309686 Jun 24 '18 at 04:59
  • Add your driver.findElement section in try catch only . Your element will not be found and it will throw exception which you can catch and print message as per your testcase . No need to get values in Boolean k .in your case it not even executing if statement , it directly catching exception after first line – Karan Jun 24 '18 at 05:04
  • I added boolean and this verification inside catch exception ,now error not shown but not displaying true or false } catch (NoSuchElementException e) { System.out.println(e); } boolean result = driver.findElement(By.xpath("")).isDisplayed(); if (result==false) System.out.println("true12"); } else { } System.out.println("false12"); } – user7309686 Jun 24 '18 at 06:16

3 Answers3

2

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

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");
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You should use the below code which will validate if at least one or more than one elements are present or not for the given xpath, before checking for the display status of the element.

List<WebElement> targetElement =  driver.findElements(By.xpath("xpath_your_expected_element"));
    try {
        if(targetElement>=1) {
            if(targetElement.isDisplayed()) {
                System.out.println("Element is present");
            }
            else {
                System.out.println("Element is found, but hidden on the page");
            }
            else {
                System.out.println("Element not found on the page");
            }
        }catch (NoSuchElementException e) {
            System.out.println("Exception in finding the element:" + e.getMessage());
        }
JaySin
  • 13
  • 3
0
        if (driver.findElements(xpath_of_element).size() != 0) return true;
        return false;