No such element exception shown if element not present

35 views
Skip to first unread message

sreedeviv...@gmail.com

unread,
Jun 23, 2018, 11:53:28 AM6/23/18
to Selenium Users
try{
boolean k= driver.findElement(By.xpath(" ")).isDisplayed();
if(!k==true)
{
System.out.println("true12"); 
 
}
}
else {
System.out.println("false12"); 
}
}catch (NoSuchElementException e) {
System.out.println(e);
         }
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.

Shiva Prasad Adirala

unread,
Jun 23, 2018, 12:00:23 PM6/23/18
to Selenium Users
If element is not found then .isDisplayed will throw and exception. I will verify the case as follow's

boolean result = false;
try {
    boolean result= driver.findElement(By.xpath(" ")).isDisplayed();
} catch (NoSuchElementException e) {
    System.out.println(e);
}

if (result) {
    System.println("true12");
} else {
    System.println("false12");

sreedeviv...@gmail.com

unread,
Jun 24, 2018, 12:47:29 AM6/24/18
to Selenium Users
but cant assign result in 2 places. boolean result=false,
boolean result =xpath

Shiva Prasad Adirala

unread,
Jun 24, 2018, 12:52:03 AM6/24/18
to Selenium Users
My bad.

Remove boolean 2nd time.
result = drive.findElem();

Reply all
Reply to author
Forward
0 new messages