1

I would like to use a if statement to ensure that an element is not present without rising a NoSuchElementException.

So for example:

if(driver.find_element_by_xpath("xpath")):
   print " This element shouldn't be there."
else: 
   print "It's all good, no element there."

How can I set my If statement to perform what I'm looking for? Is there any Selenium command for that?

2

You can do something like this:

if(len(driver.find_elements_by_xpath("xpath")) != 0):
    print "this element should not be here"
else:
    print "It is all good, no element there"
| improve this answer | |
0

Solution:

Below snippet is working well form me

if(!driver.findElements(By.name("cancel")).isEmpty()) {
  System.out.println("Element present");

}
else{
  System.out.println("Element is NOT present");
} 
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.