pythonseleniumselenium-webdriverxpath

Python Selenium Webdriver to check if element does NOT exist takes time


Trying to verify after few GUI operations some button does not exist (expected not to be present). I am using find_element_by_xpath() but its very slow. Any solution of timeout?


Solution

  • Actually WebDriver's find_element method will wait for implicit time for the element if the specified element is not found.

    There is no predefined method in WebDriver like isElementPresent() to check. You should write your own logic for that.

    Logic

    public boolean isElementPresent()
    {
       try
       {
          set_the_implicit time to zero
          find_element_by_xpath()
          set_the_implicit time to your default time (say 30 sec)
          return true;
       }
       catch(Exception e)
       {
           return false;
       }
    }