0

I'm writing some tests in java for a webpage with selenium. I see a lot of methods to ensure that an element is present on the page. But my need is to check that an element is not present on the page. More precisely, I need to check that the user has closed the pop up.

For the moment I see only methods to check that an element is present on thepage and not the contrary, like

WebElement element = driver.findElement(By.name("q"));

Do you know if methods exists to to check that an element is not present on the page ?

Thanks

  • Possible duplicate of [How do I verify that an element does not exist in Selenium 2](http://stackoverflow.com/questions/6353259/how-do-i-verify-that-an-element-does-not-exist-in-selenium-2) – Linh Nguyen May 08 '17 at 14:45

2 Answers2

1

I don't think there is some method which checks if an element is present.

However, you could do

List<WebElement> list = driver.findElements(By.name("q"));
if(list.size()==0)
    System.out.println("Not Present")

#findElements returns a list containing the matching WebElements.

dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
0

You can also try following:

public boolean isElementPresent(WebElement element){
try{
    element;
    return true;

    //element is present  
   }

catch(Exception e){

    return false;

    //element not present
   }

}
Kushal Bhalaik
  • 3,349
  • 5
  • 23
  • 46