Selenium: Handling NoSuchElementException

Seleniuim throws NoSuchElementException when it can't find the element on the screen. This blog lists down the scenarios and strategy to handle them.

Element is hidden by an overlapping <div>

This is the easiest to resolve. In case, there's a lightbox, create an action to close it before clicking on the desired element.

Page hasn't loaded

With Dojo usage, sometimes the page takes lot of time to load - at times causing "stop script" alerts as well.
To remedy this, set a page load timeout.

driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.MINUTES);

Consume the alert.

public void acceptAlertIfPresent() {
  try {
   // Check the presence of alert
   Alert alert = driver.switchTo().alert();
   // if present consume the alert
   alert.accept();
 
  } catch (NoAlertPresentException ex) {
   // Alert not present
   ex.printStackTrace();
  }
 }

Lot of Ajax processing

In an app involving lot of Ajax processing e.g. a GWT app, it's important to wait for the RPC call to finish. In such cases, an implicit timeout between action steps is required.

driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);

Comments

Popular posts from this blog

GNU Emacs as a Comic Book Reader

Tinylisp and Multi-threaded Emacs

Data Visualization with GNU Emacs