If the element is found it takes only around 3 seconds to move to the next code, but the script takes around 2.5 min to execute the steps if the element is not available. Is there any way that I could improve the code?

now = LocalDateTime.now();
logger.info("time Before IF condition confirm modal "+dtf.format(now));

if (driver.findElements(By.id("navigateAwayWarningModalYes")).size() !=0){

logger.info("Navigated from Single View");
now = LocalDateTime.now();
logger.info("time after confirm modal "+dtf.format(now));
} else {
          logger.info("Navigated from Multiple View");
          now = LocalDateTime.now();
          logger.info("time after identifying confirm modal not available 

now = LocalDateTime.now();

...enter image description here

  • Do you have an implicit wait set anywhere before you get there? if not I'd need to see what ApraisalList.clickYesFromModal does because that would be the most probable point of delay. – Daniel Nov 15 '19 at 17:17

A lot of what you are writing is custom when there are built-in methods that can help.

So, try:

  • replace your if/else statement with a try/catch block.
  • instead of using an if on driver.findElements, use driver.findElements(By.id("navigateAwayWarningModalYes")).isElementPresent() or .isVisible(). These are booleans, so you can use !driver.findElements().isElementPresent/isVisible to test if an element is not shown. Using the built-in methods will work better/faster than .size != 0
  • you can also try using the built-in waits: WebDriverWait or FluentWait to wait for an element to be present/visible or not present/visible. These also allow you to set a max timeout value and/or a polling value before an exception is thrown.
  • in your catch, you can throw an ElementNotVisibleException or another applicable exception.

If you're using Java, checkout the Javadocs here. If you are using another language, they all have applicable docs. You can also search for the above mentioned method/class names for examples.

  • I have no idea what replacing an if/else that explicitly tells what is expected with a try catch block which just fails doing something would accomplish speedwise. He still needs to pull data from the dom but instead of beng donw afterwards the system needs to raise an exception. Also isDisplayed is a function of a WebElement, not a List of WebElements so using it in conjunction with findElements will fail anyways. – Daniel Nov 15 '19 at 17:13

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.