How to handle element not found exception in Protractor

前端 未结 3 1020
长情又很酷
长情又很酷 2020-12-29 12:04

Just like Selenium webdriver provides various Exception handling for Java, is there any way we can achieve same using Protractor.

If we want to handle element not fo

3条回答
  •  伪装坚强ぢ
    2020-12-29 12:20

    Answer to this question is now in Protractor's FAQ

    How can I catch errors such as ElementNotFound?

    WebDriver throws errors when commands cannot be completed - e.g. not being able to click on an element which is obscured by another element. If you need to retry these actions, try using webdriverjs-retry. If you would just like to catch the error, do so like this

    Adapted to your question:

    elm.isPresent().then(function(present) {
      /* no webdriver js errors here */}
      if (present) {
        /* element exists */
      } else {
        /* element doesn't exist */
      }
    , function(err) {
      /* error handling here, i.e. element doesn't if got ElementNotFound
         but, eventually and less likely, other issues will fall in here too like
         NoSuchWindowsError or ElementStaleError etc...
      */
    });
    

提交回复
热议问题